Precedence of Operators:
The precedence of operator determines which operator is evaluated first if there is more than one operator in an expression. When an expression is evaluated, certain operators are performed before the others. In an expression, operators with higher precedence are evaluated before those of lower precedence.
For example,
int x=55-10*5;
In C, the precedence of * is higher than – and =. Hence, 10*5 is evaluated first. Then the expression involving – is evaluated as the precedence of – is higher than =.
Associativity of operators:
An expression can contain several operators with equal precedence. When several such operators appear at the same level in an expression, evaluation proceeds according to the associativity of the operator.
The associativity of operators determines the direction in which an expression is evaluated. All operators in C have either Left to Right associativity or Right to Left associativity.
Associativity is only used when there are two or more operators of same precedence. All operators with the same precedence have same associativity.
For example: x=1/2*3;
Here, / and * have the same precedence. Their associativity is from left to right. Hence, firstly / operation is done followed by *
The precedence and associativity of C operators is given below. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom.
Precedence |
Category |
Operator |
Associativity |
1 |
Postfix |
() [] -> . ++ — |
Left to right |
2 |
Unary |
+ – ! ~ ++ — (type)* & sizeof |
Right to left |
3 |
Multiplicative |
     *   / % |
Left to right |
4 |
additive |
+ – |
Left to right |
5 |
Shift |
<< Â Â Â >> |
Left to right |
6 |
Relational |
< Â Â <= Â Â > Â Â >= |
Left to right |
7 |
Equality |
== Â Â Â != |
Left to right |
8 |
Bitwise AND |
& |
Left to right |
9 |
Bitwise XOR |
^ |
Left to right |
10 |
Bitwise OR |
| |
Left to right |
11 |
Logical AND |
&& |
Left to right |
12 |
Logical OR |
|| |
Left to right |
13 |
Conditional |
?: |
Right to left |
14 |
Assignment |
= += -= *= /= %= >>= <<= &= ^= = |
Right to left |
15 |
Comma |
, |
Left to right |