Nearby lessons

35 of 124

C - Operator Associativity

Associativity decides the direction in which operators of equal precedence are evaluated — left to right, or right to left. Learn how it differs from precedence and why a = b = c works.

Precedence vs Associativity

These two rules work together but answer different questions:

RuleQuestion it answersExample
PrecedenceWhich operator binds tighter?In 2 + 3 * 4, * wins → 14
AssociativityFor equal precedence, which side first?In 100 / 5 * 2, left first → 40
In simple words: associativity is the tie-breaker. It only matters when two operators have the same precedence.

Left to Right — The Common Case

/ and * share the same precedence, so C groups them from the left. Getting this backwards changes the answer:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 printf("%d\n", 100 / 5 * 2); // (100/5)*2 = 40, not 100/(5*2)=10
6 printf("%d\n", 20 - 5 - 3); // (20-5)-3 = 12, not 20-(5-3)=18
7 printf("%d\n", 36 / 6 / 3); // (36/6)/3 = 2, not 36/(6/3)=18
8 return 0;
9}
Output
40
12
2

Right to Left — Assignment

Assignment is right-associative, which is exactly why you can chain it. The rightmost = runs first and its result flows leftwards:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a, b, c;
6 
7 a = b = c = 5; /* c = 5, then b = 5, then a = 5 */
8 
9 printf("a=%d b=%d c=%d\n", a, b, c);
10 
11 int x = 10;
12 x += x *= 2; /* x *= 2 first -> 20, then x += 20 -> 40 */
13 printf("x=%d\n", x);
14 return 0;
15}
Output
a=5 b=5 c=5
x=40

The Full Associativity Table

PrecedenceOperatorsAssociativity
1 (highest)() [] -> .Left to right
2! ~ ++ -- unary + - * & sizeof (type)Right to left
3* / %Left to right
4+ -Left to right
5<< >>Left to right
6< <= > >=Left to right
7== !=Left to right
8-10& then ^ then |Left to right
11-12&& then ||Left to right
13?:Right to left
14= += -= *= /= %=Right to left
15 (lowest),Left to right
Trainer's Note: Only three groups are right-to-left: unary operators, the conditional ?:, and assignment. Everything else is left to right. Memorise those three and you know the whole table.

Right-to-Left Unary Operators

Because unary operators are right-associative, they stack inwards from the variable outwards:

Example05
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int a = 5;
6 int b = - - a; /* -(-a) = 5 */
7 printf("b = %d\n", b);
8 
9 int flag = 0;
10 printf("%d\n", !!flag); /* !(!flag) = 0 */
11 
12 int c = 3;
13 printf("%d\n", - ++c); /* -(++c) = -4 */
14 return 0;
15}
Output
b = 5
0
-4

Nested Conditional Operators

?: is right-associative, so a chain reads naturally as an if/else-if ladder:

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int marks = 72;
6 
7 /* groups as: marks>=90 ? "A" : (marks>=75 ? "B" : (marks>=60 ? "C" : "D")) */
8 char *grade = marks >= 90 ? "A"
9 : marks >= 75 ? "B"
10 : marks >= 60 ? "C"
11 : "D";
12 
13 printf("Grade: %s\n", grade);
14 return 0;
15}
Output
Grade: C

What Associativity Does NOT Decide

Associativity controls grouping, not the order in which operands are actually evaluated. Those are different things, and confusing them leads to undefined behaviour:

Example07
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int i = 5;
6 
7 /* UNDEFINED BEHAVIOUR - do not write code like this.
8 C does not specify whether i++ or i is read first. */
9 // printf("%d %d\n", i++, i++);
10 // int x = i++ + i++;
11 
12 /* Safe version: one change per statement */
13 int a = i++;
14 int b = i++;
15 printf("a=%d b=%d i=%d\n", a, b, i);
16 return 0;
17}
Output
a=5 b=6 i=7

Common Mistakes

  • Assuming a - b - c means a - (b - c) — subtraction is left-associative, so it is (a - b) - c.
  • Thinking precedence sets evaluation order — in f() + g(), C may call g() first.
  • Modifying a variable twice in one expressioni++ + i++ is undefined, not merely confusing.
  • Relying on the table instead of parentheses(a + b) * c costs nothing and always reads clearly.
When in doubt, add parentheses. They generate identical machine code and remove all ambiguity for the next person reading your code — including future you.
📝 Key Takeaways
  • Precedence decides WHICH operator runs first; associativity decides the DIRECTION for ties.
  • Most operators are left-to-right.
  • Assignment, unary and the conditional operator are right-to-left.
  • a = b = c = 5 works because = is right-associative.
  • Associativity is not evaluation order of operands — that is separate and often unspecified.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4