liveBook · Manning

Sequencing

As explained in Modern C

Sequencing is the C semantic ordering relationship that constrains when evaluations and their effects occur relative to one another.

Sequencing describes when expression evaluations and their effects must occur relative to one another. C uses sequence points to serialize execution, while leaving code between them subject to permitted reordering or interleaving. A semicolon separating statements provides a simple example: the earlier statement is sequenced before the later one. section 17.2

One call order, two valid results

In printf("a = %u, b = %u\n", add(&a, &b), add(&b, &a)), the two arguments may be evaluated in either order. Each add call imposes a sequence point, so one call completes before the other, but neither call is required to come first. Starting with a = 3 and b = 5 therefore permits either a = 8, b = 13 or a = 11, b = 8. TAKEAWAY: sequencing can require an order without selecting which permitted order occurs. section 17.2

 3   unsigned add(unsigned* x, unsigned const* y) {
 4     return *x += *y;
 5   }
 6   int main(void) {
 7     unsigned a = 3;
 8     unsigned b = 5;
 9     printf("a = %u, b = %u\n", add(&a, &b), add(&b, &a));
10   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":1},{\"line\":0,\"ch\":2}],[{\"line\":4,\"ch\":20},{\"line\":4,\"ch\":21}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":15}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":15}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":0,\"ch\":31},{\"line\":0,\"ch\":45}],[{\"line\":1,\"ch\":1},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":1},{\"line\":2,\"ch\":2}],[{\"line\":5,\"ch\":20},{\"line\":5,\"ch\":21}],[{\"line\":3,\"ch\":1},{\"line\":3,\"ch\":2}],[{\"line\":3,\"ch\":5},{\"line\":3,\"ch\":8}],[{\"line\":6,\"ch\":9},{\"line\":6,\"ch\":12}],[{\"line\":3,\"ch\":9},{\"line\":3,\"ch\":13}],[{\"line\":3,\"ch\":14},{\"line\":3,\"ch\":18}],[{\"line\":4,\"ch\":1},{\"line\":4,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":15}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":5,\"ch\":1},{\"line\":5,\"ch\":2}],[{\"line\":0,\"ch\":5},{\"line\":0,\"ch\":13}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":15}],[{\"line\":5,\"ch\":7},{\"line\":5,\"ch\":15}],[{\"line\":6,\"ch\":1},{\"line\":6,\"ch\":2}],[{\"line\":6,\"ch\":7},{\"line\":6,\"ch\":13}],[{\"line\":7,\"ch\":0},{\"line\":7,\"ch\":2}]]"}
!@%STYLE%@!

Inspect the complete add and printf example; compare its two possible outputs. section 17.2

How Modern C covers it

How deep it goes
Defines sequence points and function-call ordering, while deferring additional sequencing rules beyond sequence points.
Examples
Uses add calls nested in printf arguments to demonstrate two valid evaluation orders and outputs.
What it assumes
Assumes familiarity with C statements, expressions, operands, and function calls.
What you can do after
Evaluate/understand why equivalent-looking C expressions can produce multiple permitted outcomes.
How it connects
Connects sequence points with expressions, statements, function calls, operands, and evaluation order.

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.