This is actually a fairly interesting question. It's not as simple as it looks at first. For reference, I'm going to be basing this off of the latest C11 language grammar defined in N1570
I guess the counter-intuitive part of the question is: if this is correct C:
if (a == 1) {
int b = 10;
}
then why is this not also correct C?
if (a == 1)
int b = 10;
I mean, a one-line conditional if
statement should be fine either with or without braces, right?
The answer lies in the grammar of the if
statement, as defined by the C standard. The relevant parts of the grammar I've quoted below. Succinctly: the int b = 10
line is a declaration, not a statement, and the grammar for the if
statement requires a statement after the conditional that it's testing. But if you enclose the declaration in braces, it becomes a statement and everything's well.
And just for the sake of answering the question completely -- this has nothing to do with scope. The b
variable that exists inside that scope will be inaccessible from outside of it, but the program is still syntactically correct. Strictly speaking, the compiler shouldn't throw an error on it. Of course, you should be building with -Wall -Werror
anyways ;-)
(6.7) declaration:
declaration-speci?ers init-declarator-list
opt (;)
static_assert-declaration
(6.7) init-declarator-list:
init-declarator
init-declarator-list (,) init-declarator
(6.7) init-declarator:
declarator
declarator (=) initializer
(6.8) statement:
labeled-statement
compound-statement
expression-statement
selection-statement
iteration-statement
jump-statement
(6.8.2) compound-statement:
({) block-item-listopt (})
(6.8.4) selection-statement:
(if () expression ()) statement
(if () expression ()) statement (else) statement
(switch () expression ()) statement
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…