Namespaces
Variants
Actions

Statements

From cppreference.com
< c‎ | language

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:

An attribute specifier sequence (attr-spec-seq) can be applied to an unlabeled statement, in which case (except for an expression statement) the attributes are applied to the respective statement.

(since C23)

Contents

[edit] Labels

Any statement can be labeled, by providing a name followed by a colon before the statement itself.

attr-spec-seq(optional)(since C23) identifier : (1)
attr-spec-seq(optional)(since C23) case constant-expression : (2)
attr-spec-seq(optional)(since C23) default : (3)
1) Target for goto.
2) Case label in a switch statement.
3) Default label in a switch statement.

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.

A label shall be followed by a statement.

(until C23)

A label can appear without its following statement. If a label appears alone in a block, it behaves as if it is followed by a null statement.

The optional attr-spec-seq is applied to the label.

(since C23)

[edit] Compound statements

A compound statement, or block, is a brace-enclosed sequence of statements and declarations.

{ statement | declaration...(optional) } (until C23)
attr-spec-seq(optional) { unlabeled-statement | label | declaration...(optional) } (since C23)

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