Sequence Points
A sequence point is a place in a program where every side effect from earlier evaluations has completed and no side effect from later evaluations has started. Modifying the same object twice with no sequence point between the two modifications is undefined behavior.
Detailed Explanation
C gives the compiler a lot of freedom to reorder work inside an expression. That freedom is what lets it keep values in registers, reuse partial results, and issue instructions out of source order. Sequence points are the fence posts that limit the freedom: at a sequence point, the abstract machine has to be in a consistent state, with all pending writes applied.
The most common sequence point is the end of a full expression: the semicolon at the end of a statement, the controlling expression of an if, while, or switch, each of the three parts of a for header, and the expression in a return. Inside one full expression, almost nothing is ordered unless an operator orders it.
Only four operators impose an order on their operands:
| Operator | What is ordered |
|---|---|
&& | left operand fully evaluated, then right operand (skipped if left is 0) |
|| | left operand fully evaluated, then right operand (skipped if left is non-zero) |
?: | condition fully evaluated, then exactly one branch |
, (comma operator) | left operand fully evaluated and discarded, then right operand |
There is also a sequence point after all function arguments have been evaluated and before the function body starts running.
Notice what is not on that list: +, *, =, <, and the function-call argument list. In f(a) + g(b), the compiler may call g first. In x = a() * b(), either call may run first.
The rule that bites learners is this: if a side effect on a scalar object is unsequenced relative to another side effect on that same object, or relative to a value computation that reads that object, the behavior is undefined. So i = i++ is undefined, because the write from i++ and the write from the assignment are unsequenced. f(i++, i++) is undefined for the same reason. a[i] = i++ is undefined because the read of i used to pick the array slot is unsequenced relative to the increment.
These are not "the value is one of two possibilities". They are full undefined behavior. The compiler is permitted to produce anything at all, including a value neither ordering would give.
C11 kept the term "sequence point" but rewrote the rules using three relations, which are more precise and worth learning:
- sequenced before — A finishes before B starts. This is what a sequence point creates.
- unsequenced — no order at all, and the two evaluations may interleave. This is the dangerous one.
- indeterminately sequenced — one happens entirely before the other, but which one is unspecified. Two function calls in the same expression are indeterminately sequenced.
That third category explains why total = bump() + bump() is legal even when bump mutates a global: the calls cannot interleave, so no object is written twice without ordering. You still cannot predict which runs first, so relying on the order is a portability bug, not undefined behavior.
The practical cost of all this is small once you adopt one habit: give each statement at most one side effect on any given object. Split a compound expression into two lines. The compiler will fold them back together, and the reader will thank you.
Code Examples
#include <stdio.h>
int main(void) {
int i = 5;
// Undefined behavior. Do not write any of these:
// i = i++; two unsequenced writes to i
// int bad = i++ + i++; same problem
// printf("%d %d", i++, i++); arguments are unsequenced
// int a[4]; a[i] = i++; the read of i and the write race
i = i + 1; // one write to i, well defined
printf("%d\n", i); // 6
// These operators do impose an order, so the code below is safe.
int *p = NULL;
if (p != NULL && *p > 0) { // *p is only evaluated when p is non-null
printf("unreachable\n");
}
int n = 3;
int taken = (n > 0) ? n-- : 0; // condition is sequenced before the branch
printf("taken=%d n=%d\n", taken, n); // taken=3 n=2
// The comma operator sequences left before right. Common in for headers.
for (int lo = 0, hi = 4; lo < hi; lo++, hi--) {
printf("lo=%d hi=%d\n", lo, hi);
}
return 0;
}#include <stdio.h>
static int counter = 0;
static int bump(const char *who) {
counter++;
printf("%s ran, counter is now %d\n", who, counter);
return counter;
}
int main(void) {
// The two calls are indeterminately sequenced: one completes before the
// other starts, but the standard does not say which goes first. This is
// unspecified behavior, not undefined behavior.
int total = bump("left") + bump("right");
// The printed order of the two lines above may differ between compilers
// or between -O0 and -O2. The total does not: it is always 1 + 2.
printf("total = %d\n", total); // total = 3
return 0;
}Common Gotchas
`i = i++` is undefined, not "off by one"
learners often assume it either increments or does not. The standard imposes no requirements at all here. Optimizers have been observed to produce values that neither ordering can explain, and some builds warn and then miscompile the surrounding block.
Function arguments have no evaluation order
`read_byte()` and `read_byte()` in the same argument list may run in either order. If both consume from a stream, assign to named locals on separate lines first.
`&&` sequences but `&` does not
swapping `p != NULL && p->n` for `p != NULL & p->n` compiles, drops the short circuit, and dereferences a null pointer. The same trap applies to `||` versus `|`.
C11 renamed the model without changing the verdicts
older books say "no sequence point between the modifications", C11 says "unsequenced side effects on the same scalar object". The expressions that were undefined in C89 are still undefined in C17 and C23.