塊範圍

如果識別符號的相應宣告出現在塊內(函式定義中的引數宣告適用),則識別符號具有塊作用域。範圍在相應塊的末尾結束。

具有相同識別符號的不同實體不能具有相同的範圍,但範圍可能重疊。在重疊範圍的情況下,唯一可見的範圍是在最內部範圍內宣告的範圍。

#include <stdio.h>

void test(int bar)                   // bar has scope test function block
{
    int foo = 5;                     // foo has scope test function block
    {
        int bar = 10;                // bar has scope inner block, this overlaps with previous test:bar declaration, and it hides test:bar
        printf("%d %d\n", foo, bar); // 5 10
    }                                // end of scope for inner bar
    printf("%d %d\n", foo, bar);     // 5 5, here bar is test:bar
}                                    // end of scope for test:foo and test:bar

int main(void)
{
    int foo = 3;         // foo has scope main function block

    printf("%d\n", foo); // 3
    test(5);
    printf("%d\n", foo); // 3
    return 0;
}                        // end of scope for main:foo