Concept / Given Data:
Operator precedence determines the order in which operators of different types are evaluated in an expression. Operator associativity determines the order of evaluation when operators of the same precedence appear together (left-to-right or right-to-left). Together they define the exact evaluation sequence the C compiler follows.
Step-by-Step Explanation with Examples:
Step 1 — Precedence Example (different operators):
Consider the expression:
a=5+3×2
Multiplication (×) has higher precedence than addition (+), so the compiler groups it as:
a=5+(3×2)=5+6=11
If we wanted addition first, we must use explicit parentheses: (5+3)×2=16.
Step 2 — Associativity Example (same precedence, L→R):
Consider:
b=100/10/5
Both divisions have the same precedence and are left-associative, so:
b=(100/10)/5=10/5=2
If it were right-associative (it is not), the result would be 100/(10/5)=10.
Step 3 — Right-associative Example (assignment):
Consider:
c=d=7
Assignment is right-associative, so it evaluates as:
c=(d=7)
First d receives 7, then c receives the value of the assignment expression, which is 7. Both c and d equal 7.
Step 4 — Mixed Precedence + Associativity:
e=2+3×4−6/2
Apply precedence: × and ÷ first (left-to-right among themselves):
e=2+12−3
Then + and − (left-to-right):
e=(2+12)−3=14−3=11
Precedence & Associativity Table:
Result: Precedence resolves which operator acts first; associativity resolves direction among equals. Always use parentheses to make intent explicit and avoid ambiguity in exam answers.
Tip from Examiner: In 7-mark PYQs (2023-24, 2024-25, 2025-26), examiners award 2 marks for the definition, 3 marks for a correct worked example showing grouping, and 2 marks for the table or a second example. Common mistake: writing $100/10/5 = 100/(10/5) = 10$ — this violates left-associativity and loses 3 marks. Always write the intermediate grouping in parentheses before computing the final value.