Block and Expression Statements
A block , also called a compound statement, groups a number of statements together into one statement. A block can also contain declarations.
The syntax for a block is:
{[list of declarations][list of statements]}Here is an example of a block:
{ int i = 0; /* Declarations */
static long a;
extern long max;
++a; /* Statements */
if( a >= max)
{ . . . } /* A nested block */
. . .
}The declarations in a block normally precede the statements. However, ANSI C99 permits free placement of declarations.
New blocks can occur anywhere within a function block. Usually a block is formed wherever the syntax calls for a statement, but the program requires several statements. This is the case, for example, when more than one statement is to be repeated in a loop.
An expression statement is an expression followed by a semicolon. The syntax is:
[expression] ;Here is an example of an expression statement:
y = x; // Assignment
The expression—an assignment or function call, for example—is evaluated for its side effects. The type and value of the expression are discarded.
A statement consisting only of a semicolon is called an empty statement , and does not peform any operation. For example:
for ( i = 0; str[i] != '\0'; ++i ) ; // Empty statement
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access