Names specified here
Name Description Notes Source Availability
auto Storage class for automatic objects L S Keyword C89 C90 C95 C99 C11
register Optimize for storage in register L S Keyword C89 C90 C95 C99 C11

A block, block statement or compound statement is a collection of statements to be executed in series, but treated syntactically as a single statement. It may also contain declarations necessary to fully define the contained statements. The main purposes of block statements are as follows:

A block is delimited with braces, with statements in between:

{
  statements and declarations . . .
}
compound-statement
{ block-item-list }
since C99
block-item-list
block-item
block-item-list block-item
block-item
declaration
statement

It is never terminated with a semicolon, although there are other constructs that begin and end with braces and are followed by semicolons. The statements within the block can be of all kinds, including other blocks. Such nested blocks are conventially indented to emphasize the nesting:

{
  {
    statements and declarations . . .
  }

  statements and declarations . . .

  {
    {
      statements and declarations . . .
    }
    statements and declarations . . .
  }
}

Empy blocks { } are also permitted.

Objects and functions may be declared within a block. Their names have a scope limited to that block. If they match a name declared in an outer scope, the outer name is hidden within the block in which the new name is declared.

Objects declared inside a block, without static, extern and _Thread_local, or explicitly with auto, have automatic storage duration, and are usually called local variables. Such objects are created when the block is starts to be executed, and destroyed when execution leaves the block. Note that calling a function within a block is not considered to be an exit from the block. Execution within the block is merely suspended until the function call completes and returns, and all the local variables existing just before the call still exist immediately after it.

A local variable can be declared with the keyword register, hinting to the compiler that speed is important when accessing this variable, and so the compiler might choose to hold it in some physical register. Consequently, it would not be possible to obtain the address of such an object, so the compiler will forbid it, even if it decided not to hold the variable in a register:

register int x;
int *p = &x; // error

In fact, modern compilers will probably assume register automatically if possible, and are in a better position to make the decision anyway.

Prior to C99, declarations and statements cannot be arbitrarily interleaved within a block. All declarations must come before all statements:

{
  int a; // a declaration

  printf(. . .); // a statement

  int b; // error in C95
}
compound-statement
{ declarationsopt statementsopt }
until C99
declarations
declaration
declarations declaration
statements
statement
statements statement

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000