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 goto statement) 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.


以上是Exampleoffunctionscope的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>