Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Statements

      From cppreference.com
      <c‎ |language
       
       
       
      Statements
      Labels
      Expression statements
      Compound statements
      Selection statements
      Iteration statements
      Jump 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 statementint n=1;// declaration (not a statement)    n= n+1;// expression statementprintf("n = %d\n", n);// expression statementreturn0;// return statement}// end of compound statement, end of function body


      There are five types of statements:

      1)compound statements
      2)expression statements
      3)selection statements
      4)iteration statements
      5)jump statements

      Anattribute 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 belabeled, 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)caseconstant-expression: (2)
      attr-spec-seq(optional)(since C23)default: (3)
      1) Target forgoto.
      2) Case label in aswitch statement.
      3) Default label in aswitch statement.

      Any statement (but not a declaration) may be preceded by any number oflabels, each of which declaresidentifier to be a label name, which must be unique within the enclosing function (in other words, label names havefunction 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 anull statement.

      The optionalattr-spec-seq is applied to the label.

      (since C23)

      [edit]Compound statements

      A compound statement, orblock, 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 anif statement or an iteration statement):

      if(expr)// start of if-statement{// start of blockint n=1;// declarationprintf("%d\n", n);// expression statement}// end of block, end of if-statement

      Each compound statement introduces its ownblock scope.

      The initializers of the variables with automaticstorage 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 blockputs("hello");// expression statementint n=printf("abc\n");// declaration, prints "abc", stores 4 in nint 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 endsint n=7;// n can be reused}

      [edit]Expression statements

      An expression followed by a semicolon is a statement.

      expression(optional); (1)
      attr-spec-seqexpression; (2)(since C23)

      Most statements in a typical C program are expression statements, such as assignments or function calls.

      An expression statement without an expression is called anull statement. It is often used to provide an empty body to afor orwhile loop. It can also be used to carry a label in the end of a compound statement or before a declaration:

      puts("hello");// expression statementchar*s;while(*s++!='\0');// null statement

      The optionalattr-spec-seq is applied to the expression.

      Anattr-spec-seq followed by; does not form an expression statement. It forms anattribute declaration instead.

      (since C23)

      [edit]Selection statements

      The selection statements choose between one of several statements depending on the value of an expression.

      attr-spec-seq(optional)(since C23)if(expression)statement (1)
      attr-spec-seq(optional)(since C23)if(expression)statementelsestatement (2)
      attr-spec-seq(optional)(since C23)switch(expression)statement (3)
      1)if statement
      2)if statement with an else clause
      3)switch statement

      [edit]Iteration statements

      The iteration statements repeatedly execute a statement.

      attr-spec-seq(optional)(since C23)while(expression)statement (1)
      attr-spec-seq(optional)(since C23)dostatementwhile(expression); (2)
      attr-spec-seq(optional)(since C23)for(init-clause;expression(optional);expression(optional))statement (3)
      1)while loop
      2)do-while loop
      3)for loop

      [edit]Jump statements

      The jump statements unconditionally transfer flow control.

      attr-spec-seq(optional)(since C23)break; (1)
      attr-spec-seq(optional)(since C23)continue; (2)
      attr-spec-seq(optional)(since C23)returnexpression(optional); (3)
      attr-spec-seq(optional)(since C23)gotoidentifier; (4)
      1)break statement
      2)continue statement
      3)return statement with an optional expression
      4)goto statement

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.8 Statements and blocks (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.8 Statements and blocks (p: 106-112)
      • 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

      [edit]See also

      C++ documentation forStatements
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/statements&oldid=179343"

      [8]ページ先頭

      ©2009-2025 Movatter.jp