Precedence And Associativity In C


Operators precedence:


It defines the order of evaluation of operators in an expression. Associativity defines the order in which operators of the same precedence are evaluated in an expression.

Associativity can be either left to right or right to left.

Operator precedent example:

10 + 20 * 30

Now here addition operator has lower precedence and multiplication operator has higher precedence, there for the multiplication operator evaluate first and our equation became 10 + 60 is equal to 70

Here 10+20*30 is calculated as 10+(20*30) is equal to 70

Operator Associativity:

Example:

'*' and '/' have the same precedence and their associativity is left to right.

100/10*10 is evaluated as (100/10)*10 here / and * have the same precedence but left to right associativity so division operator evaluate first because it comes first from the left side and then the multiplication operator is evaluated

100/10  *10

10* 10

100



Example:


100 + 100/10 - 2 *10

will be evaluated as follows

Step 1: 100 +100/10 - 2*10

Step 2: 100 + 10 - 2*10

Step 3: 100 +10 -20

Step 4: 110 - 20

Step 5: 90

All operator with the same precedence have same associativity

Associativity is only used when there are two or more operator of same precedence

Precedence and associativity of prefix ++ and postfix ++ are different.

Precedence of postfix ++ is more than prefix ++, their associativity is also different.

Associativity of postfix ++ is left to right and Associativity prefix ++ is right to left.

Comma has the least precedence among all operators and should be used carefully.


 

Precedence And Associativity Table:




Operator


Description  

Associativity


( )
[ ]
.
->
++ —
    Parentheses (function call) (see Note 1)
    Brackets (array subscript)
    Member selection via object name
    Member selection via pointer
    Postfix increment/decrement (see Note 2)                                               

left-to-right


++ —
+ –
! ~
(type)
*
&
sizeof
    Prefix increment/decrement
    Unary plus/minus
    Logical negation/bitwise complement
    Cast (convert value to temporary value of type)
    Dereference
    Address (of operand)
    Determine size in bytes on this implementation
right-to-left
*  /  %     Multiplication/division/modulus left-to-right
+  –     Addition/subtraction left-to-right
<<  >>     Bitwise shift left, Bitwise shift right left-to-right
<  <=
>  >=
    Relational less than/less than or equal to
    Relational greater than/greater  than or equal to
left-to-right
==  !=     Relational is equal to/is not equal to left-to-right
&     Bitwise AND       left-to-right      
^     Bitwise exclusive OR left-to-right
|     Bitwise inclusive OR left-to-right
&&     Logical AND  left-to-right
| |     Logical OR left-to-right
? :     Ternary conditional right-to-left
=
+=  -=
*=  /=
%=  &=
^=  |=
<<=  >>=
    Assignment
    Addition/subtraction assignment
    Multiplication/division assignment
    Modulus/bitwise AND assignment
    Bitwise exclusive/inclusive OR assignment
    Bitwise shift left/right assignment
right-to-left

,

                               
    Comma (separate expressions) left-to-right