范围和可见性Scope and Visibility

标识符的“可见性”确定其可以引用的程序部分,即其“范围”。An identifier's "visibility" determines the portions of the program in which it can be referenced — its "scope." 标识符仅在其“范围”包含的程序部分中可见(即可使用),这可能仅限于(按限制增长的顺序)它显示在其中的文件、函数、块或函数原型。An identifier is visible (i.e., can be used) only in portions of a program encompassed by its "scope," which may be limited (in order of increasing restrictiveness) to the file, function, block, or function prototype in which it appears. 标识符的范围是可使用名称的程序的一部分。The scope of an identifier is the part of the program in which the name can be used. 这有时被称为“词法范围”。This is sometimes called "lexical scope." 有四种范围:函数、文件、块和函数原型。There are four kinds of scope: function, file, block, and function prototype.

除标签之外,所有标识符的范围都由在其上进行声明的级别决定。All identifiers except labels have their scope determined by the level at which the declaration occurs. 以下针对每种范围的规则将管理标识符在程序中的可见性:The following rules for each kind of scope govern the visibility of identifiers within a program:

文件范围File scope
带文件范围的标识符的声明符或类型说明符显示在任何块或参数列表的外部,并且可以从其声明后的翻译单元中的任何位置进行访问。The declarator or type specifier for an identifier with file scope appears outside any block or list of parameters and is accessible from any place in the translation unit after its declaration. 带文件范围的标识符名称通常称为“全局”或“外部”。Identifier names with file scope are often called "global" or "external." 全局标识符的范围开始于其定义或声明的点,结束于翻译单元的末尾。The scope of a global identifier begins at the point of its definition or declaration and terminates at the end of the translation unit.

函数范围Function scope
标签是唯一一种具有函数范围的标识符。A label is the only kind of identifier that has function scope. 通过在语句中使用标签来隐式声明标签。A label is declared implicitly by its use in a statement. 标签名称在函数中必须是唯一的。Label names must be unique within a function. (有关标签和标签名称的详细信息,请参阅 goto 和 Labeled 语句。)(For more information about labels and label names, see The goto and Labeled Statements.)

块范围Block scope
带块范围的标识符的声明符或类型说明符显示在块中或函数定义中的形参声明列表中。The declarator or type specifier for an identifier with block scope appears inside a block or within the list of formal parameter declarations in a function definition. 它仅从其声明或定义的点到包含其声明或定义的块的结尾可见。It is visible only from the point of its declaration or definition to the end of the block containing its declaration or definition. 其范围限制为该块以及嵌入该块中的任何块,并结束于封闭该关联块的大括号处。Its scope is limited to that block and to any blocks nested in that block and ends at the curly brace that closes the associated block. 此类标识符有时称为“局部变量”。Such identifiers are sometimes called "local variables."

函数原型范围Function-prototype scope
带函数原型范围的标识符的声明符或类型说明符显示在函数原型(不是函数声明的一部分)中的参数声明列表中。The declarator or type specifier for an identifier with function-prototype scope appears within the list of parameter declarations in a function prototype (not part of the function declaration). 其范围在函数声明符的末尾终止。Its scope terminates at the end of the function declarator.

存储类别中介绍了可使变量在其他源文件中可见的适当声明。The appropriate declarations for making variables visible in other source files are described in Storage Classes. 但是,使用 static 存储类说明符在外部级别声明的变量和函数仅在定义它们的源文件中可见。However, variables and functions declared at the external level with the static storage-class specifier are visible only within the source file in which they are defined. 所有其他函数都是全局可见的。All other functions are globally visible.

另请参阅See Also

生存期、范围、可见性和链接Lifetime, Scope, Visibility, and Linkage