C Operator Precedence and Associativity
What is Operator Precedence?
Operator precedence is used to determine the order of operators
evaluated in an expression. In c programming language every operator
has precedence (priority). When there is more than one operator in an
expression the operator with higher precedence is evaluated first and
the operator with the least precedence is evaluated last.
What is Operator Associativity?
Operator associativity is used to determine the order of operators with
equal precedence evaluated in an expression. In the c programming
language, when an expression contains multiple operators with equal
precedence, we use associativity to determine the order of evaluation
of those operators.
In c programming language the operator precedence and associativity
are as shown in the following table.
Precedence Operator Operator Meaning Associativity
1 () function call Left to Right
[] array reference
-> structure member access
. structure member access
2 ! negation Right to Left
~ 1's complement
+ Unary plus
Precedence Operator Operator Meaning Associativity
- Unary minus
++ increment operator
-- decrement operator
& address of operator
* pointer
sizeof returns size of a variable
(type) type conversion
3 * multiplication Left to Right
/ division
% remainder
4 + addition Left to Right
- subtraction
5 << left shift Left to Right
>> right shift
6 < less than Left to Right
<= less than or equal to
> greater than
>= greater than or equal
to
7 == equal to Left to Right
!= not equal to
8 & bitwise AND Left to Right
Precedence Operator Operator Meaning Associativity
9 ^ bitwise EXCLUSIVE OR Left to Right
10 | bitwise OR Left to Right
11 && logical AND Left to Right
12 || logical OR Left to Right
13 ?: conditional operator Left to Right
14 = assignment Right to Left
*= assign multiplication
/= assign division
%= assign remainder
+= assign addition
-= assign subtraction
&= assign bitwise AND
^= assign bitwise XOR
|= assign bitwise OR
<<= assign left shift
>>= assign right shift
15 , separator Left to Right
In the above table, the operator precedence decreases from top to
bottom and increases from bottom to top.