Precedence and Associativity
Understanding operator precedence and associativity is crucial in C programming, as it determines the order in which expressions are evaluated. This helps in writing correct and efficient code. Below is a detailed explanation of both concepts.
1. Operator Precedence
Operator precedence defines the order in which different operators in an expression are evaluated. Operators with higher precedence are evaluated before those with lower precedence. Here’s a general hierarchy of operator precedence in C, from highest to lowest:
- Highest Precedence:
- Postfix Operators:Â
()
 (function call),Â[]
 (array subscript),Â->
 (structure pointer),Â.
 (structure member) - Unary Operators:Â
++
 (increment),Â--
 (decrement),Â+
 (unary plus),Â-
 (unary minus),Â!
 (logical NOT),Â~
(bitwise NOT),Â*
 (dereference),Â&
 (address-of),Âsizeof
, type castsÂ(type)
- Postfix Operators:Â
- Intermediate Precedence:
- Multiplicative Operators:Â
*
,Â/
,Â%
- Additive Operators:Â
+
,Â-
- Bitwise Shift Operators:Â
<<
,Â>>
- Multiplicative Operators:Â
- Relational Operators:Â
<
,Â<=
,Â>
,Â>=
- Equality Operators:Â
==
,Â!=
- Bitwise AND:Â
&
- Bitwise XOR:Â
^
- Bitwise OR:Â
|
- Logical AND:Â
&&
- Logical OR:Â
||
- Conditional Operator:Â
?:
- Assignment Operators:Â
=
,Â+=
,Â-=
,Â*=
,Â/=
,Â%=
,Â<<=
,Â>>=
,Â&=
,Â^=
,Â|=
- Comma Operator:Â
,
 (lowest precedence)
2. Operator Associativity
Associativity defines the order in which operators of the same precedence level are evaluated. Operators can be left-to-right or right-to-left in their evaluation.
- Left-to-Right Associativity:
- Operators that associate from left to right are evaluated in the order they appear from left to right. Most binary operators, such as arithmetic and bitwise operators, fall into this category.
- Right-to-Left Associativity:
- Operators that associate from right to left are evaluated in the reverse order. Assignment operators and the conditional (ternary) operator (
?:
) are examples of this.
- Operators that associate from right to left are evaluated in the reverse order. Assignment operators and the conditional (ternary) operator (
3. Detailed Table of Precedence and Associativity
Here’s a comprehensive table summarizing the precedence and associativity of all operators in C:
Precedence Level | Operator | Description | Associativity |
---|---|---|---|
1 | () [] -> . | Function call, array subscript, structure access | Left-to-right |
2 | ++ -- (postfix) | Postfix increment and decrement | Left-to-right |
3 | ++ -- (prefix), + - ! ~ * & sizeof | Prefix increment/decrement, unary operators, type casts | Right-to-left |
4 | * / % | Multiplication, division, modulo | Left-to-right |
5 | + - | Addition, subtraction | Left-to-right |
6 | << >> | Bitwise left shift, right shift | Left-to-right |
7 | < <= > >= | Relational operators | Left-to-right |
8 | == != | Equality operators | Left-to-right |
9 | & | Bitwise AND | Left-to-right |
10 | ^ | Bitwise XOR | Left-to-right |
11 | ` | ` | Bitwise OR |
12 | && | Logical AND | Left-to-right |
13 | | | Logical OR | Left-to-right |
14 | ?: | Ternary conditional operator | Right-to-left |
15 | = += -= *= /= %= >>= <<= &= ^= ` | Assignment operators | Left-to-right |
16 | , | Comma operator | Left-to-right |