TRT
Sign in
easyFoundationsconcept

What is the difference between operator precedence and order of evaluation in C, and why is i = i++ undefined behavior?

Separate compile-time parse tree grouping (precedence) from runtime subexpression execution order, and explain unsequenced side effects.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstOperators, Precedence, and Evaluation OrderDifferentiate operator precedence from evaluation order, master short-circuit evaluation, and eliminate unsequenced modification bugs.

A strong answer

Operator precedence is a compile-time parsing rule that determines how operands group with operators—for example, in a() + b() * c(), multiplication has higher precedence than addition, so the expression parses as a() + (b() * c()). However, order of evaluation is a runtime question: in what temporal sequence does the CPU call a(), b(), and c() before combining their results? In C, the order of evaluation of most subexpressions and function arguments is unspecified—the compiler is free to call c(), then a(), then b() if that produces better register allocation.

Because evaluation order is unspecified, modifying a scalar object more than once—or modifying it and reading it for an independent purpose—without an intervening sequence point causes undefined behavior. In expressions like i = i++ or arr[i] = i++, the variable i has two unsequenced side effects (or an unsequenced read and write) in the same full expression. The C standard does not merely say the result is implementation-defined; it declares the entire program's behavior undefined, allowing optimizing compilers to emit arbitrary code.

Only four operators in C guarantee strict left-to-right evaluation with a sequence point between their operands: logical AND (&&), logical OR (||), the ternary conditional operator (? :), and the comma operator (,).

What the interviewer is listening for

  • Clearly separates syntax grouping (precedence/associativity) from runtime execution order (evaluation order).
  • States that function argument evaluation order (f(g(), h())) is unspecified in C.
  • Explains why unsequenced modifications (i = i++, f(i++, i++)) trigger undefined behavior rather than just picking one order.

Common mistakes

  • Claiming that higher operator precedence (* before +) forces the operands of * to be evaluated before the operands of + at runtime.
  • Confusing the comma operator a(), b() (which guarantees left-to-right sequencing) with the commas separating function arguments f(a(), b()) (which do not).
  • Writing clever single-line expressions with multiple ++ or -- operators on the same variable instead of splitting them into separate statements.

This answer walks

Practice follow-ups

  1. 01Which four operators in C explicitly guarantee left-to-right evaluation and introduce a sequence point?
  2. 02In f(a(), b()), does C guarantee whether a() or b() runs first?

Not started

Sign in to save your learning progress.

Sign in to save