Eric Sosman said:
luserXtrog said:
luserXtrog wrote:
[...]
Isn't it in the grammar that a block (except for a function def) is a
special case of a statement which happens not to take terminating
semicolon? I'm too lazy to look it up, but that's the shape it took in
my memory. And this meme has yet to lead me astray.
A "block" is not a statement; it is a collection of statements
and declarations (6.8p3). Some kinds of statements contain blocks
as part or all of their makeup: The compound statement in its entirety
is a block (6.8.2), the if and switch statements are blocks and can
contain subordinate blocks (6.8.4p3).
How did you find those sections without reading any of the surrounding
material? Everything is a statement.
Nonsense. `int i = 42;', for example, is not a statement.
`int main(void) { return 0; }' is not a statement.
Section 6.8.2, entitled "Compound statement", describes how a block
may be used wherever a statement is required.
6.8.2p1 gives the syntax of a compound statement, and 6.8.p2 says
in its entirety "A compound statement is a block," and that's all there
is in 6.8.2. Where is the description you, er, describe?
"A compound statement is a block" does not imply that all blocks are
compound statements. For example, `if (a) b=42; else c=24;' has three
blocks, none of them compound statements (6.8.4p3).
You're right. I've always assumed that "block" and "compound
statement" are synonymous, but they're not. But I dislike the way the
standard expresses this.
C99 6.8p2 purports to define the term "block":
A _block_ allows a set of declarations and statements to be
grouped into one syntactic unit. The initializers of objects that
have automatic storage duration, and the variable length array
declarators of ordinary identifiers with block scope, are
evaluated and the values are stored in the objects (including
storing an indeterminate value in objects without an initializer)
each time the declaration is reached in the order of execution, as
if it were a statement, and within each declaration in the order
that declarators appear.
But this definition doesn't really define what is and is not a block.
I should be able to read a definition for a term and be able to
determine unambiguously whether a given thing is an example of that
term or not, perhaps reading other text to understand the meanings of
terms used in the definition. The above is a statement about blocks;
it's not a definition of the term.
6.8.2p2 says:
A _compound statement_ is a block.
6.8.4p3 says:
A selection statement is a block whose scope is a strict subset of
the scope of its enclosing block. Each associated substatement is
also a block whose scope is a strict subset of the scope of the
selection statement.
6.8.5p5 says:
An iteration statement is a block whose scope is a strict subset
of the scope of its enclosing block. The loop body is also a block
whose scope is a strict subset of the scope of the iteration
statement.
So a statement is a block if it's a compound statement, a selection
statement or a substatement thereof, or an iteration statement or a
substatement thereof. In the following:
if (condition)
return 1;
return 0;
"return 1;" is a block but "return 0;" isn't.
What's the point? Certain declarations have block scope, but you
can only have a declaration within a compound statement anyway; this:
if (x == 3)
int y;
is illegal. (That's not quite true; a for statement can have a
declaration.) If the language definition were changed so that only a
compound statement is a block, what, if anything, would break?