Statements
Statements are fragments of the C program that are executed in sequence. The body of any function is a compound statement, which, in turn is a sequence of statements and declarations:
int main(void) { // start of a compound statement int n = 1; // declaration (not a statement) n = n+1; // expression statement printf("n =%d\n", n); // expression statement return 0; // return statement } // end of compound statement, end of function body
There are five types of statements:
Labels
Any statement can be labeled, by providing a name followed by a colon before the statement itself.
identifier : statement
|
(1) | ||||||||
case constant_expression : statement
|
(2) | ||||||||
default : statement
|
(3) | ||||||||
Any statement (but not a declaration) may be preceded by any number of labels, each of which declares identifier to be a label name, which must be unique within the enclosing function (in other words, label names have function scope).
Label declaration has no effect on its own, does not alter the flow of control, or modify the behavior of the statement that follows in any way.
Compound statements
A compound statement, or block, is a brace-enclosed sequence of statements and declarations.
{ statement | declaration...(optional) }
|
(1) | ||||||||
The compound statement allows a set of declarations and statements to be grouped into one unit that can be used anywhere a single statement is expected (for example, in an if statement or an iteration statement):
if (expr) // start of if-statement { // start of block int n = 1; // declaration printf("%d\n", n); // expression statement } // end of block, end of if-statement
Each compound statement introduces its own block scope.
The initializers of the variables with automatic storage duration declared inside a block and the VLA declarators are executed when flow of control passes over these declarations in order, as if they were statements:
int main(void) { // start of block { // start of block puts("hello"); // expression statement int n = printf("abc\n"); // declaration, prints "abc", stores 4 in n int a[n*printf("1\n")]; // declaration, prints "1", allocates 8*sizeof(int) printf("%zu\n", sizeof(a)); // expression statement } // end of block, scope of n and a ends int n = 7; // n can be reused }
Expression statements
An expression followed by a semicolon is a statement.
expression(optional) ;
|
(1) | ||||||||
Most statements in a typical C program are expression statements, such as assignments or function calls.
An expression statement without an expression is called a null statement. It is often used to provide an empty body to a for or while loop. It can also be used to carry a label in the end of a compound statement or before a declaration:
puts("hello"); // expression statement char *s; while (*s++ != '\0') ; // null statement
Selection statements
The selection statements choose between one of several statements depending on the value of an expression.
if ( expression ) statement
|
(1) | ||||||||
if ( expression ) statement else statement
|
(2) | ||||||||
switch ( expression ) statement
|
(3) | ||||||||
Iteration statements
The iteration statements repeatedly execute a statement.
while ( expression ) statement
|
(1) | ||||||||
do statement while ( expression ) ;
|
(2) | ||||||||
for ( init_clause ; expression(optional) ; expression(optional) ) statement
|
(3) | ||||||||
Jump statements
The jump statements unconditionally transfer flow control.
break ;
|
(1) | ||||||||
continue ;
|
(2) | ||||||||
return expression(optional) ;
|
(3) | ||||||||
goto identifier ;
|
(4) | ||||||||
References
- C11 standard (ISO/IEC 9899:2011):
- 6.8 Statements and blocks (p: 146-154)
- C99 standard (ISO/IEC 9899:1999):
- 6.8 Statements and blocks (p: 131-139)
- C89/C90 standard (ISO/IEC 9899:1990):
- 3.6 STATEMENTS