PRECEDENCE LEVEL:
- Till now we completed almost all the operators, so we know each and every operator’s functionality.
- But what if there are three or more operators in a single expression like 5*6+8-5/4
- If we indicate parenthesis the expression becomes
((5*6)+((8-5)/4)) then first 5*6=30 and next 8-5=3,
((30)+((3)/4)) then 3/4=0,
(30+0) then 30+0=30
(30) final result is 30.
- But what if we don't denote the parenthesis for 5*6+8-5/4, what result is obtained? How the expression is calculated?
- For such type of calculations C has certain rules to follow, each and every operation is calculated based upon the precedence level or the priority level of the operators.
- The precedence chart is shown below.
- The operator with highest priority according to precedence chart is calculated first.
- If an expression has operators with same precedence then they are calculated based on associativity.
Operator |
Description |
Associativity |
( ) |
Parentheses |
left-to-right |
++ -- |
Prefix increment/decrement |
right-to-left |
* / % |
Multiplication/division/modulus |
left-to-right |
+ - |
Addition/subtraction |
left-to-right |
<< >> |
Left shift, Right shift operator |
left-to-right |
< <= |
Less than/Less than or equal to |
left-to-right |
== != |
Equal to/ not equal to |
left-to-right |
& |
Bitwise AND |
left-to-right |
^ |
Exclusive OR Operator |
left-to-right |
| |
OR Operator |
left-to-right |
&& |
Logical AND Operator |
left-to-right |
| | |
Logical OR Operator |
left-to-right |
? : |
Conditional Operator |
right-to-left |
= |
Assignment Operator |
right-to-left |
, |
Comma operator |
left-to-right |
No comments:
Post a Comment