Operator Precedence and Associativity in C: The Table, the Traps and Solved Output Questions

Parse C expressions before evaluating them, using a focused precedence ladder and worked output questions on increments, assignment and comma.

KnowledgeGate Team

Exam prep & CS education

Updated 22 Jul 20266 min read

C output questions often look like memory tests, but they are parsing tests. The compiler groups an expression by precedence and associativity before values are computed. Once you can add the same parentheses, most of the answer is ordinary arithmetic.

Precedence groups, associativity breaks ties

Precedence decides which operator binds more tightly. In 2 + 3 * 4, multiplication binds tighter than addition, so the grouping is 2 + (3 * 4).

Associativity decides how operators at the same precedence level group. Binary addition and subtraction group left to right, so 10 - 3 - 2 means (10 - 3) - 2, which is 5. Assignment groups right to left, so x = y = 5 means x = (y = 5).

Neither rule generally specifies the order in which operands are evaluated. Precedence gives a syntax tree. Evaluation sequencing is a separate part of the C rules. That distinction matters whenever function calls, increments or other side effects occur.

Use this three-step discipline:

  1. Tokenise the expression.

  2. Insert parentheses from the precedence ladder and associativity.

  3. Evaluate the grouped expression, checking sequencing before combining side effects.

The C operator precedence ladder, highest to lowest

Read this ladder downward. An operator on a higher rung binds tighter than every operator below it, and the associativity column decides ties inside one rung.

Level

Operators

Associativity

1

postfix ++, postfix --, function call (), array []

Left to right

2

unary +, -, !, ~, prefix ++, prefix --, sizeof, cast

Right to left

3

*, /, %

Left to right

4

binary +, -

Left to right

5

<, <=, >, >=

Left to right

6

==, !=

Left to right

7

bitwise &

Left to right

8

bitwise ^

Left to right

9

bitwise |

Left to right

10

logical &&

Left to right

11

logical ||

Left to right

12

conditional ?:

Right to left

13

assignment =, +=, -=, *=, /= and related forms

Right to left

14

comma ,

Left to right

Postfix increment sits much higher than binary addition. Equality sits above bitwise AND. Assignment is near the bottom. Those three relative positions explain several famous traps.

Two more operator groups slot into the same ladder without disturbing its order. The shift operators << and >> sit between rung 4 and rung 5, so 1 << 2 + 3 means 1 << (2 + 3), which is 32 and not the 7 you would get by shifting first. Member access . and -> belongs on rung 1 beside postfix ++, above every unary operator, which is why *p.next is read as *(p.next). Unary * and &, for dereference and address of, share rung 2 with the other unary operators.

A numbered precedence ladder for C running from rung 1 at the top to rung 14 at the bottom: postfix ++ and --, function call and array subscript; unary + - ! ~ with prefix ++ and --, sizeof and cast; * / %; binary + and -; < <= > >=; == and !=; bitwise &; bitwise ^; bitwise |; logical &&; logical ||; the conditional ?:; assignment; comma. Every rung is marked left to right except the unary rung, the conditional rung and the assignment rung, which are marked right to left. Beside the ladder, the expression 2 + 3 * 4 > 10 && 5 is reduced in four arrowed steps: 3 * 4 = 12, then 2 + 12 = 14, then 14 > 10 = 1, then 1 && 5 = 1.

Worked example one: a precedence chain

Evaluate:

int x = 2 + 3 * 4 > 10 && 5;

Insert the implied grouping:

int x = (((2 + (3 * 4)) > 10) && 5);

Now calculate one precedence level at a time:

  1. 3 * 4 = 12

  2. 2 + 12 = 14

  3. 14 > 10 = 1

  4. 1 && 5 = 1, because both operands are non-zero

Therefore x = 1. The final result of C's logical AND is an integer 0 or 1, not one of the original operand values.

Check the parse independently by reading the ladder downward: multiplication before addition, addition before relational comparison, and relational comparison before logical AND. The same answer follows.

Worked example two: a+++b and maximal munch

Consider:

int a = 5, b = 2;

int c = a+++b;

The lexer follows the maximal-munch rule: at each point it forms the longest valid token. The character stream a + + + b therefore becomes:

[a] [++] [+] [b]

It does not become [a] [+] [++] [b]. The tokenised expression groups as:

(a++) + b

Postfix a++ yields a's old value, 5, as the value used in the addition. The side effect then changes a to 6. Thus:

  • c = 5 + 2 = 7

  • a = 6

  • b = 2

The printed values are a = 6, b = 2, c = 7.

The tokenisation of a+++b with top row character stream a + + + b, second row maximal-munch tokens [a] [++] [+] [b], third row grouping ((a++) + b), leaves labelled "a=5, postfix yields 5 then a becomes 6" and "b=2", and root result 7.

Worked example three: comma and chained assignment

Set a = 1 and b = 2, then evaluate:

int c = (a, b);

The parentheses make (a, b) a comma expression. It evaluates a, discards that value, evaluates b, and yields the right operand's value. Therefore c = 2.

The parentheses are important because commas also separate declarators. In int c = a, b;, the declaration grammar declares c with initializer a and separately declares b; it is not an initializer containing the comma operator.

Now take:

x = y = 5;

Assignment is right-associative, so the grouping is x = (y = 5). The inner assignment sets y to 5 and itself yields 5. The outer assignment then sets x to 5. Both finish at 5.

The traps that turn into wrong answers

Precedence is not evaluation order

i = i++ + ++i; has undefined behaviour in C because modifications and value computations involving i are unsequenced relative to one another. Operator precedence can show the grouping, but it cannot create a safe order for those side effects. Do not guess a numeric output.

Equality binds tighter than bitwise AND

if (a & b == c) parses as:

if (a & (b == c))

It does not mean (a & b) == c. Parenthesise the intended bitwise computation explicitly.

Assignment can be a condition

if (x = 5) assigns 5 to x, then tests that non-zero value, so the condition is true. If comparison was intended, write if (x == 5). Many compilers warn about the assignment, but the syntax itself is valid.

Tokens come before precedence

The parser never gets a chance to interpret a+++b as a + (++b) because the lexer has already created the ++ token after a. Maximal munch happens first.

How output questions are set

These expressions appear in campus-placement aptitude rounds, C semester exams, lab vivas and fresher coding screens. Distractors usually correspond to a specific misparse: reversing associativity, placing & above ==, treating postfix increment as prefix increment, or reading a declaration comma as an operator.

For interview-style drills, work through C Programming Interview Questions for Freshers. If your target includes recruitment or eligibility exams, C Programming for CS Teaching Exams connects the same language rules to that question style.

On every output problem, write the tokens and parentheses before touching the values. If the expression changes one object more than once without sequencing, stop and classify the behaviour rather than inventing an execution order. Once the grouping is settled, the operand types decide the arithmetic that follows, and that half of the same question family is worked out in Data Types and Operators in C.

The short version and the next step

Precedence chooses the tighter operator. Associativity breaks a tie at one level. Neither is a general rule for operand evaluation order. Tokenise, parenthesise, check sequencing, then compute.

KnowledgeGate's question bank holds about 1,000 C-programming questions, and more than a hundred of them sit on operators and expressions alone, which is exactly where increment, comma and assignment traps live. Build the language systematically with the C Programming course, then use the Coding Skills category to continue into structured practice. The ladder becomes useful only when you apply it until the parentheses are automatic.