Ads

Precedence Level


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
Brackets
dot operator to access members
access members via pointer
Postfix increment/decrement

left-to-right

++ --
+ -
! ~
(type)
*
&
sizeof

Prefix increment/decrement
Unary plus/minus
Logical negation/bitwise complement
Type cast
Dereference
Address operator
size of operator

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
Greater than/greater 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
Addition/subtraction (Compound)
Multiplication and division(Compound)
Modulus and bitwise AND (Compound)
Exclusive OR and inclusive OR (Compound)
Left shift and right shift (Compound)

right-to-left

,

Comma operator

left-to-right






No comments:

Post a Comment