5.1.5C Programming

Operator precedence — full table

1,913 words9 min readdifficulty · medium6 backlinks

WHAT precedence and associativity actually mean


HOW to read the full table

The table goes from highest precedence (binds tightest, top) to lowest (binds loosest, bottom).

Lvl Operators Description Assoc
1 () [] -> . ++ --(postfix) calls, subscript, member, postfix → L→R
2 ++ --(prefix) + -(unary) ! ~ *(deref) &(addr) sizeof (type) unary ← R→L
3 * / % multiplicative L→R
4 + - additive L→R
5 << >> bit shift L→R
6 < <= > >= relational L→R
7 == != equality L→R
8 & bitwise AND L→R
9 ^ bitwise XOR L→R
10 | bitwise OR L→R
11 && logical AND L→R
12 || logical OR L→R
13 ?: ternary conditional ← R→L
14 = += -= *= /= %= &= ^= |= <<= >>= assignment ← R→L
15 , comma L→R
Figure — Operator precedence — full table

Deriving the parse tree from scratch

Let's derive the grouping of a + b * c - d step by step instead of memorising it.

  1. List operators by precedence found: * (lvl 3), + and - (lvl 4). Why this step? We attack highest precedence first; it forms the innermost group.
  2. Group * first: a + (b*c) - d. Why? * outranks both + and -, so it claims b and c before anyone else.
  3. Now + and - tie (both lvl 4) → use associativity = L→R: group left first: (a + (b*c)) - d. Why? Left-associative means the leftmost equal-precedence operator binds first.

Final tree: ((a + (b*c)) - d). Done — uniquely determined.


Worked examples



Recall Feynman: explain to a 12-year-old

Imagine kids in a tug-of-war over a toy (a number). Some kids are "stronger" (higher precedence) and grab the toy first. × is stronger than +. If two kids are equally strong, we use a rule: usually "the one on the left wins" (left-to-right), but for a few like "=" the one on the right wins. That's it — precedence is strength, associativity is the left/right tie-breaker. The computer uses these rules so every math sentence has exactly one meaning.


Active recall

What two layers resolve operator ambiguity in C?
Precedence (binding strength) and associativity (tie-break for equal precedence).
Does precedence determine order of evaluation?
No — only grouping/parse-tree shape, not the time order operands are computed.
How does x & 1 == 0 parse?
As x & (1 == 0) because == (lvl 7) outranks & (lvl 8).
Associativity of assignment operators?
Right-to-left, so a = b = 0 means a = (b = 0).
Which is higher, && or ||?
&& is higher; a || b && c means a || (b && c).
How does 1 << 2 + 3 evaluate?
As 1 << (2+3) = 32, since + outranks <<.
Postfix ++ vs unary -: which binds tighter?
Postfix ++ (lvl 1) binds tighter than unary - (lvl 2), so -a++ is -(a++).
Associativity of the ternary ?:?
Right-to-left, so a?b:c?d:e is a?b:(c?d:e).
Which operator has the lowest precedence in C?
The comma operator ,.
Why is if (a = 5) a common bug?
= is assignment (returns 5, truthy), not comparison ==; precedence doesn't fix it — it's a different operator.

Connections

  • Expressions and Statements in C
  • Bitwise Operators
  • Logical Operators and Short-circuit Evaluation
  • Sequence Points and Undefined Behaviour
  • Type Casting and Conversions
  • The Ternary Conditional Operator

Concept Map

resolved by

layer 1

layer 2

decides

tie-breaks

builds

L→R or R→L

is NOT

controlled by

surprise

R→L example

causes

almost lowest

Ambiguous text expr

C rulebook

Precedence

Associativity

Which op binds tighter

Equal precedence ops

Unique parse tree

Order of evaluation

Sequence points

Bitwise below equality

a = b = 0

Common bugs

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, jab tum likhte ho a + b * c, toh humare liye confusion ho sakta hai ki pehle kya hoga, par compiler ke paas ek fixed rulebook hota hai. Us rulebook mein do cheezein hain: precedence (kaunsa operator zyada "strong" hai, jaise * is stronger than +) aur associativity (jab do operator equal strong hon, toh left se chalein ya right se). Bas yahi do rules se har expression ka ek hi unique meaning ban jaata hai.

Sabse important baat: precedence sirf grouping decide karta hai, timing nahi. Matlab f() + g() * h() mein g()*h() ek group banega, par C zaroori nahi pehle g() hi call kare — order alag baat hai. Isliye jab order matter kare toh sequence points (&&, ||, ,, ;) use karo, warna undefined behaviour ho sakta hai (jaise i = i++ + 1).

Exam aur real bugs mein 80/20 yeh hai: bitwise & ^ | actually == != se neeche hote hain, isliye x & 1 == 0 galat parse hota hai as x & (1==0) — hamesha bracket lagao: (x & 1) == 0. Aur && is higher than || (jaise multiply before add), aur assignment = lowest aur right-to-left hota hai, isliye a = b = 5 ka matlab a = (b = 5). In teen-chaar facts ko ratt lo, baaki table reference ke liye hai.

Yaad rakhne ka simple tarika: precedence = "taqat" (strength), associativity = "left ya right wala jeeta". Jab confuse ho, toh bracket laga do — brackets level 1 hain, sabko override kar dete hain. Safe code likho, smart code nahi.

Go deeper — visual, from zero

Test yourself — C Programming

Connections