Operator precedence and associativity in C

Order of evaluation of operations can alter the final result. For example, consider evaluating following expression without any mathematics rule.

5 + 3 * 4 / 2

If you do not follow any specific rule to evaluate such expressions, then you probably lead to ambiguous result. Such as one might think it will evaluate either 32 or 11 or 18.5 etc.

To overcome such ambiguity, in mathematics we follow BODMAS rule. Similarly, in computer programming we follow operator precedence and associativity rule.

Precedence of operators

Precedence of an operator specifies its priority compared to other operator. If an expression contain different types of operator, then precedence of operators specifies the order of evaluation each operator.

For example, consider the given expression
int x = 5 + 4 * 2;

The result of above expression is 13 instead of 18. Since * operator has higher precedence than + operator. Hence, + operator is evaluated after * operator.

Now consider the above expression again with slight change
int x = (5 + 4) * 2;
This time result of above expression is 18. Because precedence of ( ) operator is more than * operator. Thus ( ) operator evaluates first to 5 + 4 = 9. Then * operator evaluates to 9 * 2 = 18.

Precedence of an operator affects result of an expression. But, what if two operators have same precedence? This ambiguity is resolved by associativity of operators.

Associativity of operators

If an expression contains more than one operator with same precedence. Then operator precedence along with its associativity defines the order of evaluation of expression.

Operator associativity can either be left-to-right or right-to-left. Means if an expression contains two or more operators of same precedence. Then they are evaluated in either left to right or right to left order.

Consider the below expression
int x = 5 * 4 / 4 % 3;

In the above expression precedence of all three operators are same. Hence, the result of expression is ambiguous (if we follow only precedence). The result can either be 2 or 20 etc. However, order of evaluation ambiguity is resolved if we follow precedence along with associativity rule. All the three operators are left to right associated, hence the expression is guaranteed to evaluate 2.

Consider another operator that associates from right-to-left.
x = y = z;

In the above expression the right-most operator is evaluated first, proceeding towards left. First the value z is assigned to y then the value of y is assigned to x.

Important note: Operators with same precedence have same associativity.

In C programming compilers follow operator precedence and associativity rule to resolve order of evaluation of operators.

Operator precedence and associativity table

OperatorDescriptionPrecedenceAssociativity
()Parentheses1Left to right
[]Array element reference
->Member access via pointer
.Member access via object name
++Postfix increment
Postfix decrement
!Logical NOT operator2Right to left
~Bitwise complement operator
+Unary plus operator
Unary minus operator
++Prefix Increment
Prefix Decrement
*Indirection operator
sizeof()Size of operator
(type)Type cast
.*Dereference operator3Left to right
->*Dereference operator
*Multiply4Left to right
/Divide
%Modulus
+Binary addition5Left to right
Binary subtraction
<<Bitwise left shift6Left to right
>>Bitwise right shift
<Less than7Left to right
<=Less than or equal to
>Greater than
>=Greater than or equal to
==Equal to8Left to right
!=Not equal to
&Bitwise AND9Left to right
^Bitwise XOR10Left to right
|Bitwise OR11Left to right
&&Logical AND12Left to right
||Logical OR13Left to right
?:Conditional operator14Right to left
=Simple assignment15Right to left
*=Assign product
~=Assign bitwise complement
/=Assign quotient
%=Assign remainder
+=Assign sum
-=Assign difference
&=Assign bitwise AND
^=Assign bitwise XOR
|=Assign bitwise OR
<<=Assign bitwise left shift
>>=Assign bitwise right shift
,Comma operator16Left to right