Exampleoffunctionscope
The following would be an example of block scope:
int func(int a) {
int b;
...
} // end of block scope for a & b
What would be an example of function scope, which I believe is applied to goto labels?
回答
From the C standard, 6.2.1 Scopes of identifiers:
A label name is the only kind of identifier that has function scope. It can be used (in a
gotostatement) anywhere in the function in which it appears, and is declared implicitly by its syntactic appearance (followed by a:and a statement).
The reason it is different than block scope is that it ignores blocks:
void f(int a) {
{
increase_a: ++a;
}
if (a)
goto increase_a;
}
This compiles because increase_a is visible by the goto statement, which wouldn't be the case if it had block scope.