C 存储类C Storage Classes

变量的“存储类”可确定项是具有“全局”还是“本地”生存期。The "storage class" of a variable determines whether the item has a "global" or "local" lifetime. C 将这两个生存期称为“静态”和“自动”。C calls these two lifetimes "static" and "automatic." 具有全局生存期的项存在且具有贯穿整个程序执行过程的值。An item with a global lifetime exists and has a value throughout the execution of the program. 所有函数都具有全局生存期。All functions have global lifetimes.

每次执行控制权传递到从中定义它们的块时,都会为自动变量或具有本地生存期的变量分配新存储。Automatic variables, or variables with local lifetimes, are allocated new storage each time execution control passes to the block in which they are defined. 当执行返回时,这些变量不再具有有意义的值。When execution returns, the variables no longer have meaningful values.

C 提供了以下存储类说明符:C provides the following storage-class specifiers:

语法Syntax

storage-class-specifierstorage-class-specifier:
autoauto

registerregister

staticstatic

externextern

typedeftypedef

__declspec ( extended-decl-modifier-seq ) /* Microsoft 专用 */__declspec ( extended-decl-modifier-seq ) /* Microsoft Specific */

__declspec 之外,只能在声明中的 declaration-specifier 中使用一个 storage-class-specifier。Except for __declspec, you can use only one storage-class-specifier in the declaration-specifier in a declaration. 如果没有制定存储类规范,块中的声明将创建自动对象。If no storage-class specification is made, declarations within a block create automatic objects.

使用 auto 或 register 说明符声明的项具有本地生存期。Items declared with the auto or register specifier have local lifetimes. 使用 static 或 extern 说明符声明的项具有全局生存期。Items declared with the static or extern specifier have global lifetimes.

由于 typedef__declspec 与其他四个 storage-class-specifier 终端的语义不同,因此将分开讨论它们。Since typedef and __declspec are semantically different from the other four storage-class-specifier terminals, they are discussed separately. 有关 typedef 的特定信息,请参阅 Typedef 声明For specific information on typedef, see Typedef Declarations. 有关 __declspec 的特定信息,请参阅扩展的存储类特性For specific information on __declspec, see Extended Storage-Class Attributes.

源文件中变量和函数声明的位置还会影响存储类和可见性。The placement of variable and function declarations within source files also affects storage class and visibility. 所有函数定义之外的声明据说显示在“外部级别”。Declarations outside all function definitions are said to appear at the "external level." 函数定义中的声明显示在“内部级别”。Declarations within function definitions appear at the "internal level."

每个存储类说明符的确切含义取决于两个因素:The exact meaning of each storage-class specifier depends on two factors:

另请参阅See Also

声明和类型Declarations and Types