Operators precedence in C
Need a fast revision version? See the C Programming Cheat Sheet →Table of Contents
- What is Operator Precedence?
- What is Associativity?
- The Complete Precedence Table
- Precedence vs Associativity
- Solved Examples
- Common Mistakes
- Parentheses Best Practices
- Practice MCQs
- Key Takeaways
1. What is Operator Precedence?
Operator precedence determines which operator is evaluated first when an expression contains multiple operators of different types.
Think of it like the order of operations in mathematics: multiplication happens before addition. In C, * has higher precedence than +, so 10 + 5 * 2 evaluates to 20, not 30.
Example
int x = 10 + 5 * 2;
Evaluation:
*has higher precedence than+.5 * 2 = 10is evaluated first.10 + 10 = 20.- Output:
20
If precedence did not exist (or if + had higher precedence), the result would be 30.
Why Precedence Matters
Without precedence rules, the same expression could yield different results on different compilers or platforms. Precedence ensures consistent, predictable evaluation across all C implementations.
2. What is Associativity?
When an expression contains multiple operators of the same precedence, associativity determines the direction of evaluation — left-to-right or right-to-left.
Example: Left-to-Right Associativity
int x = 20 / 5 * 2;
Both / and * have the same precedence and are left-to-right associative.
Evaluation:
20 / 5 = 4(leftmost operation first)4 * 2 = 8- Output:
8
If they were right-to-left associative, the result would be 2 (20 / (5 * 2)).
Example: Right-to-Left Associativity
int x = 5;
int y = 10;
int z = 20;
x = y = z;
The assignment operator = is right-to-left associative.
Evaluation:
y = zis evaluated first:ybecomes20.x = yis evaluated next:xbecomes20.- All three variables now hold
20.
If assignment were left-to-right associative, x = y would happen first (x = 10), then y = z (y = 20), leaving x as 10 and y as 20.
3. The Complete Precedence Table
Full Table (Highest to Lowest Precedence)
| Precedence | Operator(s) | Description | Associativity |
|---|---|---|---|
| 1 | () [] -> . | Function call, array subscript, member access | Left to Right |
| 2 | ++ -- (postfix) | Postfix increment/decrement | Left to Right |
| 3 | ++ -- (prefix) + - (unary) ! ~ * & sizeof _Alignof (type) | Prefix increment/decrement, unary plus/minus, logical/bitwise NOT, dereference, address-of, sizeof, cast | 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 comparisons | Left to Right |
| 8 | == != | Equality comparisons | Left to Right |
| 9 | & | Bitwise AND | Left to Right |
| 10 | ^ | Bitwise XOR | Left to Right |
| 11 | | | Bitwise OR | Left to Right |
| 12 | && | Logical AND | Left to Right |
| 13 | || | Logical OR | Left to Right |
| 14 | ?: | Ternary conditional | Right to Left |
| 15 | = += -= *= /= %= &= |= ^= <<= >>= | Assignment operators | Right to Left |
| 16 | , | Comma operator | Left to Right |
Simplified Memory Aid
For most practical programming, remembering this simplified order is sufficient:
() -- Parentheses (highest)
Unary -- ! ~ ++ -- + - * & sizeof (type)
* / % -- Multiplicative
+ - -- Additive
<< >> -- Shift
< <= > >= -- Relational
== != -- Equality
& -- Bitwise AND
^ -- Bitwise XOR
| -- Bitwise OR
&& -- Logical AND
|| -- Logical OR
?: -- Ternary
= -- Assignment
, -- Comma (lowest)
Mnemonic
“Please Excuse My Dear Aunt Sally” adapted for C:
- Parentheses
- Unary
- Multiply / Divide
- Add / Subtract
- Shift
- Relational
- Equality
- Bitwise (AND, XOR, OR)
- Logical (AND, OR)
- Ternary
- Assignment
- Comma
4. Precedence vs Associativity
| Concept | Determines | When Applied |
|---|---|---|
| Precedence | Which operator is evaluated first | When operators have different precedence levels |
| Associativity | Evaluation direction (left/right) | When operators have the same precedence level |
Visual Example
int result = 10 + 20 * 30 / 40;
Step 1: Precedence decides the grouping
* and / have higher precedence than +.
10 + (20 * 30 / 40)
Step 2: Associativity decides the order within the group
* and / have the same precedence and are left-to-right associative.
10 + ((20 * 30) / 40)
Step 3: Evaluate
20 * 30 = 600600 / 40 = 1510 + 15 = 25
5. Solved Examples
Example 1: Arithmetic Precedence
int a = 2 + 3 * 4;
Evaluation:
3 * 4 = 12
2 + 12 = 14
Output: 14
Example 2: Same Precedence, Left-to-Right
int a = 20 - 8 / 2;
Evaluation:
8 / 2 = 4
20 - 4 = 16
Output: 16
Example 3: Mixed Arithmetic and Logical
int a = 1;
int b = 2;
int c = 3;
a = a && b * c + 3;
Evaluation:
b * c = 6 (multiplication first)
6 + 3 = 9 (addition next)
1 && 9 = 1 (logical AND last)
Output: a = 1
Arithmetic operators are evaluated before logical operators.
Example 4: Relational vs Arithmetic
int x = 10 > 5 + 3;
Evaluation:
5 + 3 = 8
10 > 8 = 1 (true)
Output: 1
Example 5: Equality vs Arithmetic
int x = 5 == 2 + 3;
Evaluation:
2 + 3 = 5
5 == 5 = 1 (true)
Output: 1
Example 6: Bitwise vs Logical
int x = 5 & 3 == 1;
Evaluation:
3 == 1 = 0 (equality has higher precedence than &)
5 & 0 = 0
Output: 0
Common trap: Many programmers expect this to be
(5 & 3) == 1, which would be1 == 1 = 1. But==has higher precedence than&!
Example 7: Assignment Associativity
int x = 5;
x = x = 2 + 3;
Evaluation:
2 + 3 = 5
x = 5 (rightmost assignment first)
x = 5 (left assignment next)
Output: x = 5
Assignment operators associate right to left.
Example 8: Ternary Operator
int a = 5, b = 10, c = 15;
int result = a > b ? a : b > c ? b : c;
Evaluation:
Ternary ?: is right-to-left associative.
Step 1: b > c ? b : c = 10 > 15 ? 10 : 15 = 15
Step 2: a > b ? a : 15 = 5 > 10 ? 5 : 15 = 15
Output: 15
Example 9: Complex Mixed Expression
int x = 10 == 5 + 5 && 8 > 3;
Evaluation:
5 + 5 = 10
10 == 10 = 1
8 > 3 = 1
1 && 1 = 1
Output: 1
Example 10: Shift vs Addition
int x = 1 + 2 << 3;
Evaluation:
+ and << — which has higher precedence? << (shift) has higher precedence than + (addition)? No! Addition (+) has precedence 5, while shift (<<) has precedence 6. So << has lower precedence than +.
Wait, let me re-check: Looking at the table, * / % are precedence 4, + - are precedence 5, << >> are precedence 6. So << has higher precedence than +.
1 + (2 << 3) = 1 + 16 = 17
Output: 17
6. Common Mistakes
Mistake 1: Assuming Addition Before Multiplication
10 + 2 * 5
Wrong assumption: (10 + 2) * 5 = 60
Correct: 10 + (2 * 5) = 20
Mistake 2: Assuming Logical AND Before Arithmetic
a && b + c
Wrong assumption: (a && b) + c
Correct: a && (b + c)
Arithmetic operators have higher precedence than logical operators.
Mistake 3: Confusing Bitwise and Logical Precedence
5 & 3 == 1
Wrong assumption: (5 & 3) == 1 = 1 == 1 = 1
Correct: 5 & (3 == 1) = 5 & 0 = 0
== has higher precedence than &.
Mistake 4: Misunderstanding Assignment Associativity
x = y = z = 5;
Wrong assumption (left-to-right):
x = y(x becomes old y)y = z(y becomes 5)
Correct (right-to-left):
z = 5y = z(y becomes 5)x = y(x becomes 5)
All three become 5.
Mistake 5: Ternary Operator Nesting
int result = a > b ? a : b > c ? b : c;
Wrong assumption (left-to-right):
a > b ? a : bevaluated first- Then
result > c ? ...
Correct (right-to-left):
b > c ? b : cevaluated first- Then
a > b ? a : (result of step 1)
Mistake 6: Confusing = and ==
if (x = 5) { } // Assignment, not comparison!
This assigns 5 to x and evaluates to 5 (true). The condition is always true.
Correct:
if (x == 5) { }
7. Parentheses Best Practices
Rule: When in Doubt, Use Parentheses
Parentheses have the highest precedence and override all other rules. They make code more readable and prevent subtle bugs.
Good Examples
// Clear intent
int result = (a + b) * c;
// Explicit grouping
if ((ptr != NULL) && (ptr->value > 0)) { }
// Complex condition
if ((x > 0) && (x < 100) && ((x % 2) == 0)) { }
// Bitwise with comparison
if ((flags & MASK) == EXPECTED) { }
Bad Examples (Ambiguous)
// Ambiguous: what was intended?
int result = a + b * c << d;
// Dangerous: & vs == precedence
if (flags & MASK == VALUE) { }
// Confusing: mixed logical operators
if (a && b || c && d) { }
Refactored (Good)
// Explicit grouping
int result = (a + (b * c)) << d;
// Safe: explicit precedence
if ((flags & MASK) == VALUE) { }
// Clear: explicit grouping
if ((a && b) || (c && d)) { }
8. Practice MCQs
MCQ 1
printf("%d", 2 + 3 * 4);
A. 20
B. 14
C. 24
D. 10
Answer: B (14)
3 * 4 = 12, then 2 + 12 = 14.
MCQ 2
printf("%d", 20 / 5 * 2);
A. 2
B. 4
C. 8
D. 10
Answer: C (8)
/ and * have same precedence, left-to-right: 20 / 5 = 4, then 4 * 2 = 8.
MCQ 3
int a = 1;
int b = 2;
int c = 3;
printf("%d", a && b * c + 3);
A. 0
B. 1
C. 9
D. 6
Answer: B (1)
b * c = 6, 6 + 3 = 9, 1 && 9 = 1.
MCQ 4
printf("%d", 5 == 2 + 3);
A. 0
B. 1
C. 5
D. Error
Answer: B (1)
2 + 3 = 5, 5 == 5 = 1.
MCQ 5
printf("%d", 10 > 5 + 10);
A. 0
B. 1
C. 5
D. Error
Answer: A (0)
5 + 10 = 15, 10 > 15 is false = 0.
MCQ 6
int x;
x = 5 + 2 > 6;
printf("%d", x);
A. 5
B. 6
C. 1
D. 0
Answer: C (1)
5 + 2 = 7, 7 > 6 is true = 1.
MCQ 7
int x = 10;
x = x > 5 && 3 * 2;
printf("%d", x);
A. 0
B. 1
C. 6
D. 10
Answer: B (1)
3 * 2 = 6, x > 5 = 10 > 5 = 1, 1 && 6 = 1.
MCQ 8
int a;
a = 2 + 3 * 4 > 10;
printf("%d", a);
A. 14
B. 0
C. 1
D. 12
Answer: C (1)
3 * 4 = 12, 2 + 12 = 14, 14 > 10 is true = 1.
MCQ 9
int x;
x = 10 == 5 + 5 && 8 > 3;
printf("%d", x);
A. 0
B. 1
C. 5
D. Error
Answer: B (1)
5 + 5 = 10, 10 == 10 = 1, 8 > 3 = 1, 1 && 1 = 1.
MCQ 10
int x = 5;
x = x = 2 + 3;
printf("%d", x);
A. 2
B. 3
C. 5
D. Error
Answer: C (5)
Assignment is right-to-left: x = (2 + 3) = 5, then x = 5.
9. Key Takeaways
- Precedence determines which operator is evaluated first when operators have different precedence levels.
- Associativity determines the evaluation direction when operators have the same precedence level.
- Parentheses
()have the highest precedence and should be used liberally to make expressions clearer. - Arithmetic operators (
*,/,%,+,-) are evaluated before relational operators (<,>,==, etc.), which are evaluated before logical operators (&&,||). - Assignment operators (
=,+=, etc.) are right-to-left associative. - The ternary operator
?:is right-to-left associative. - Most binary operators (arithmetic, relational, bitwise, logical) are left-to-right associative.
- When in doubt, always use parentheses — clarity is more important than brevity.
- The most common precedence traps involve:
&vs==,=vs==, and mixing bitwise with logical operators.
End of Notes — Continue to MCQs or Flashcards