存储类Storage Class

函数定义中的存储类说明符为函数提供 extern 或 static 存储类。The storage-class specifier in a function definition gives the function either extern or static storage class.

语法Syntax

function-definition:function-definition:
declaration-specifiers optattribute-seq optdeclarator declaration-list optcompound-statementdeclaration-specifiers optattribute-seq optdeclarator declaration-list optcompound-statement

/* attribute-seq 为 Microsoft 专用 //* *attribute-seq is Microsoft Specific */

declaration-specifiersdeclaration-specifiers:
storage-class-specifier declaration-specifiers optstorage-class-specifier declaration-specifiers opt

type-specifier declaration-specifiers opttype-specifier declaration-specifiers opt

type-qualifier declaration-specifiers opttype-qualifier declaration-specifiers opt

storage-class-specifier: /* 针对函数定义 */storage-class-specifier: /* For function definitions */
externextern

staticstatic

如果函数定义不包括 storage-class-specifier,则存储类默认为 externIf a function definition does not include a storage-class-specifier, the storage class defaults to extern. 您可以将函数显式声明为 extern,但这不是必需的。You can explicitly declare a function as extern, but it is not required.

如果函数的声明包含 storage-class-specifier extern,则标识符拥有与带文件范围的标识符的任何可见声明相同的链接。If the declaration of a function contains the storage-class-specifier extern, the identifier has the same linkage as any visible declaration of the identifier with file scope. 如果没有带文件范围的可见声明,则标识符具有外部链接。If there is no visible declaration with file scope, the identifier has external linkage. 如果标识符具有文件范围但没有 storage-class-specifier,则标识符具有外部链接。If an identifier has file scope and no storage-class-specifier, the identifier has external linkage. 外部链接意味着,标识符的每个实例表示相同的对象或函数。External linkage means that each instance of the identifier denotes the same object or function. 有关链接和文件范围的详细信息,请参阅生存期、范围、可见性和链接See Lifetime, Scope, Visibility, and Linkage for more information about linkage and file scope.

extern 之外的存储类说明符的块范围数声明将生成错误。Block-scope function declarations with a storage-class specifier other than extern generate errors.

带 static 存储类的函数仅在定义它的源文件中可见。A function with static storage class is visible only in the source file in which it is defined. 所有其他函数(无论是显式还是隐式地为它们提供 extern 存储类)在程序中的所有源文件中都可见。All other functions, whether they are given extern storage class explicitly or implicitly, are visible throughout all source files in the program. 如果需要 static 存储类,则必须在函数声明(如果有)的第一个匹配项和函数定义中声明该存储类。If static storage class is desired, it must be declared on the first occurrence of a declaration (if any) of the function, and on the definition of the function.

Microsoft 专用Microsoft Specific

在启用 Microsoft 扩展时,如果函数定义在同一源文件中且定义显式指定 static 存储类,则会为最初未使用存储类(或使用 extern 存储类)声明的函数提供 static 存储类。When the Microsoft extensions are enabled, a function originally declared without a storage class (or with extern storage class) is given static storage class if the function definition is in the same source file and if the definition explicitly specifies static storage class.

当使用 /Ze 编译器选项进行编译时,使用 extern 关键字在块内声明的函数具有全局可见性。When compiling with the /Ze compiler option, functions declared within a block using the extern keyword have global visibility. 在使用 /Za 编译时,并非如此。This is not true when compiling with /Za. 如果需要考虑源代码的可移植性,则不应依赖此功能。This feature should not be relied upon if portability of source code is a consideration.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

C 函数定义C Function Definitions