liveBook · Manning

Compound statement

As explained in Modern C, Third Edition

A compound statement is a C statement consisting of a brace-delimited sequence of statements treated as one statement.

A compound statement is a brace-delimited sequence of statements treated as one statement. Braces therefore let a function body or control-flow body contain several operations. section 2.1

A loop body as one statement

for (int i = 0; i < 5; ++i) { printf("%d\n", i); record(i); } The compound statement after for is the loop body. The for statement repeats the entire brace-delimited body, not merely its first line, for i values 0 through 4. section 2.4.1

Use compound statements to make control-flow structure explicit:

  • Place the repeated operations of a for loop inside { ... }. section 2.4.1
  • Define iteration variables in the initial clause of the for statement so their scope stays narrow. section 2.4.1
  • Use the same compound-statement form for while and do loop bodies when several statements must execute together. section 3.2
for (size_t i = 10; i; --i) {
  something(i);
}
for (size_t i = 0, stop = upper_bound(); i < stop; ++i) {
  something_else(i);
}
for (size_t i = 9; i <= 9; --i) {
  something_else(i);
}

Compare compound loop bodies with scoped iteration variables across several for forms. section 3.2

How Modern C, Third Edition covers it

How deep it goes
Explains grouping and loop-body behavior with syntax and scope advice, but does not descend into compiler implementation.
Examples
Uses function bodies, repeated printf operations, counted loops, inverse approximation loops, and scoped iteration variables.
What it assumes
Assumes basic C syntax and introduces the relationship between statements, function bodies, control flow, and scope.
What you can do after
Apply compound statements to group repeated operations and keep loop-variable definitions local to for loops.
How it connects
Connects compound statements with function definitions, for loops, while loops, do loops, statements, and scope.

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.