liveBook · Manning

Sequence point

As explained in Modern C, Third Edition

A sequence point is a point in C execution at which the language requires specified evaluations and side effects to be completed or ordered relative to other evaluations.

A sequence point is a language-defined point that serializes execution: evaluations and side effects before it are completed or ordered relative to those after it. Code between sequence points may be evaluated in different orders, interleaved, or governed by other sequencing constraints. A semicolon separating statements provides the simplest example. section 19.2

Sequence points do not necessarily choose an order between expressions; they require that some order exists. Function argument expressions are unsequenced, while function calls can impose sequence points. These relaxed rules support compiler optimization and avoid imposing unnecessary evaluation orders. section 19.2

Two valid outcomes

In printf("...", add(&a, &b), add(&b, &a)), either add call may execute first. Starting with a = 3 and b = 5 therefore permits a = 8, b = 13 or a = 11, b = 8. section 19.2

unsigned add(unsigned* x, unsigned const* y) [[unsequenced]] {
  return *x += *y;
}
int main(void) {
  unsigned a = 3;
  unsigned b = 5; 
  printf("a = %u, b = %u\n", add(&a, &b), add(&b, &a));
}

Inspect the modifying add function and its two unsequenced uses as printf arguments. section 19.2

 a = 8, b = 13

See the output produced when the first add call executes before the second. section 19.2

 a = 11, b = 8

See the alternative output produced when the second add call executes first. section 19.2

How Modern C, Third Edition covers it

How deep it goes
Defines serialization and demonstrates alternative outcomes, while deferring additional sequencing rules beyond sequence points.
Examples
Uses modifying add calls passed to printf and shows two permitted final-value outputs.
What you can do after
Evaluate which execution orders and results remain permitted when expressions are unsequenced.
How it connects
Connects sequence points with function arguments, function calls, evaluation order, sequencing, and compiler optimization.

Also explained in

Compare how 2 books explain sequence point →

Related concepts

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