On Feb 26, 3:37 pm, (e-mail address removed) (Kenny McCormack)
wrote:
At the outset, IANAL, so I do not belong to the target audience (as
indicated in your subject). That said, I suspect that those can
quote chapter and verse in this group have most likely kill-filed
you that they may not have seen your post at all.
Correct.
His assumption is incorrect when the curly braces don't begin and
terminate a compound statement. For example, you cannot drop the curly
braces in the following:
int array[1] = {1};
Even when the curly brackets do mark the beginning and end of a compound
statement, his assumption still can fail if the only "thing" inside a
compound statement is a declaration. That's because removing the curly
brackets would replace a statement with with a declaration. Most
locations where compound statements are allowed are NOT locations where
declarations are allowed (see the grammar summary in A.2.3), and that's
what went wrong in his example code below. The only exceptions are
within another compound statement (6.8.2p1), and in function definitions
(as he notes below).
No, there's no such requirement. Neither of the following switch
statements is a syntax error, nor do they violate any constraints.
switch(i++) j = 3;
switch(j) case 5: i = 4;
Those statements are functionally equivalent to:
i++;
if(j==5) i = 4;
which is a much better way to write such code - but there's nothing
wrong with those switch() statements, as far as the C standard is
concerned, just because they are don't have any curly brackets.
If the compound statement controlled by a switch() contained only a
single declaration, removing the curly brackets would be a syntax error,
for the same reason as for the if() statement below .
Given that assumption, the program should compile. The problem is with
the assumption, which is false.
In particular, the grammar rules for if statements say:
if ( expression ) statement
or
if ( expression ) statement else statement
"int j;" is a declaration, so it it doesn't match either form.
{int j;} is a compound statement, and therefore a statement (6.8p1), so
it matches the first grammar rule for if statements.
One may have to get down to the grammar and see what the productions
say.
Definitely. Talking about "stuff" and "things" is just not sufficiently
precise to resolve questions like this.
It is apparent that while a declaration such as -
int i = 0, j = 0;
- is allowed outside of any function, a statement such as -
i = j;
- is not allowed outside any function. So, there must be something in
the grammar that distinguishes a declaration from a statement.
Lots of things, mostly in section 6.8.
I suspect the grammar might say that the statement following an if
condition cannot be composed of just a declaration alone, but must
either be a block or a statement.
Actually, no - it's simpler than that - a compound statement is a
statement (6.8p1), so "statement" is sufficient.
All compound statements are blocks (6.8.2), but so are selection
statements and their associated sub statements (6.8.4p3), iteration
statements and the bodies of their loops (6.8.5p5), even if none of
those things is a compound statement.
Therefore, it actually works the other way around: in the statement
if(i) j = 3;
the sub-statement "j=3" is a block, because of it's position in the if
statement.