liveBook · Manning

Evaluation order

As explained in Modern C

Evaluation order is the sequence in which a C expression's operands and subexpressions are computed.

Consider printf("%g and %g\n", f(a), f(b));. C does not specify whether f(a) or f(b) is evaluated first. If either call has side effects, the result can depend on compiler choices, options, or surrounding code. section 4.5

printf("%g and %g\n", f(a), f(b));


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}]]"}
!@%STYLE%@!

Use the call expression to inspect the two argument expressions whose relative evaluation order is unspecified. section 4.5

TAKEAWAY: distinguish the operators that impose order from those that do not.

  • &&, ||, ?:, and the comma operator evaluate their first operand first. section 4.5
  • The comma operator evaluates operands left to right and produces the right operand's value; do not confuse it with commas separating function arguments or initializations. section 4.5
  • Most operators and function calls do not sequence their operands or argument expressions. section 4.6
  • Avoid side effects in functions called inside expressions, and do not use the comma operator in clean code. section 4.5

How Modern C covers it

How deep it goes
Explains sequencing rules, side-effect consequences, compiler variability, and the comma operator without descending into implementation internals.
Examples
Uses f(a) and f(b) in printf arguments and A[i, j] to expose unspecified ordering and comma-operator behavior.
What you can do after
Apply evaluation-order rules by avoiding side effects inside expressions and refusing to rely on unspecified sequencing.
How it connects
Connects short-circuit operators, the comma operator, function calls, arithmetic operators, and side effects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.