Namespaces
Variants

File scope

From cppreference.net

如果声明该标识符的声明符或类型说明符出现在任何块或参数列表之外,则该标识符具有文件作用域,其作用范围终止于翻译单元的末尾。

因此,将标识符的声明(在声明符或类型说明符中)放置在任何块或参数列表之外,意味着该标识符具有文件作用域。标识符的文件作用域从其声明处开始,延伸到该声明所在的翻译单元末尾。

示例

标识符 a、b、f 和 g 具有文件作用域。

#include <stdio.h>
int a = 1;
static int b = 2;
void f (void) {printf("from function f()\n");}
static void g (void) {printf("from function g()\n");}
int main(void)
{
    f();
    g();
    return 0;
}
/* end of this translation unit, end of file scope */

可能的输出:

from function f()
from function g()