C Reference ยท Operators and Expressions
Sequence points and evaluation order in C
Reference for C sequence points and evaluation order: which operators sequence their operands, unspecified argument order and why i = i++ is undefined.
Operator precedence tells you how an expression is grouped; it does not tell you in which order the pieces run. C leaves the order of evaluation of most operands and of function arguments unspecified. The compiler may pick any order, and may pick a different one at a different optimization level.
The language controls this with sequencing. A sequence point (or, in the wording of recent standards, a "sequenced before" relation) guarantees that all side effects of earlier evaluations are complete before later ones start. If you modify an object twice, or modify it and read it for a purpose other than computing the new value, with nothing sequencing the two accesses, the behavior is undefined.
int i = 1; i = i++ + 1; /* undefined behavior: i modified twice, unsequenced */ a[i] = i++; /* undefined behavior */ f(g(), h()); /* g and h called in unspecified order */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
There is no syntax: this is a set of rules. Evaluations are sequenced at these points:
| Where | Guarantee |
|---|---|
| End of a full expression | Statement expression, the controlling expression of if, switch, while, do, each clause of for, return expression, an initializer. |
&&, || |
The left operand is fully evaluated, with side effects, before the right operand. |
?: |
The condition is evaluated and its effects completed before the chosen branch. |
, (comma operator) |
The left operand completes before the right. |
| Function call | All argument evaluations complete before the function body starts (but arguments are not sequenced relative to each other). |
| End of a full declarator | In int a = f(), b = a + 1; the initializer of a is complete before b's. |
Everywhere else, for example operands of +, *, ==, = (the two sides), and the arguments of one call, the order is unspecified or unsequenced.
Errors / undefined behavior: if a scalar object is modified twice, or modified and also read other than to determine the new value, and the accesses are unsequenced, the behavior is undefined. The compiler is allowed to produce any result, including different ones in different places.
Usage notes
- Unspecified is not undefined. In
f(g(), h())either order is valid and the program is fine as long as the result does not depend on the order.i = i++is undefined: the program may do anything. - Classic undefined examples:
i = i++,a[i] = i++,x = x++ + ++x,printf("%d %d", i, i++). Do not try to work out "what it prints": compilers differ, and one compiler differs between-O0and-O2. - Safe on one object:
i = i + 1,i += 2andx = x * 2readxonly to compute the value stored back. The assignment's own side effect is sequenced after its operand values are computed. - One side effect per expression. The easiest rule: in a single expression, modify a given variable at most once and do not read it elsewhere in the same expression.
- Function arguments with side effects (
next()) should go in separate statements if their order matters. - Short-circuit and conditional operators are the tools for guarded evaluation; see short-circuit evaluation.
- Assignment is not left-to-right. In
a[f()] = g()the call order is unspecified. - Compiler help: GCC and Clang warn about many cases with
-Wsequence-point(part of-Wall), but not all. Sanitizers do not find them reliably either. - Newer wording. C11 describes this with "sequenced before", "indeterminately sequenced" and "unsequenced", the same ideas as classic sequence points.