extern 存储类说明符extern Storage-Class Specifier

使用 extern 存储类说明符声明的变量是对名称与在程序的任何源文件的外部级别定义的名称相同的变量的引用。A variable declared with the extern storage-class specifier is a reference to a variable with the same name defined at the external level in any of the source files of the program. 内部 extern 声明用于使外部级别变量定义在块中可见。The internal extern declaration is used to make the external-level variable definition visible within the block. 除非在外部级别另有声明,否则使用 extern 关键字声明的变量仅在声明它的块中可见。Unless otherwise declared at the external level, a variable declared with the extern keyword is visible only in the block in which it is declared.

示例Example

此示例阐释内部和外部级别的声明:This example illustrates internal- and external-level declarations:

// extern_StorageClassSpecified.c  
#include <stdio.h>  

void other( void );  

int main()  
{  
    // Reference to i, defined below:   
    extern int i;  

    // Initial value is zero; a is visible only within main:   
    static int a;  

    // b is stored in a register, if possible:   
    register int b = 0;  

    // Default storage class is auto:   
    int c = 0;  

    // Values printed are 1, 0, 0, 0:   
    printf_s( "%d\n%d\n%d\n%d\n", i, a, b, c );  
    other();  
    return;  
}  

int i = 1;  

void other( void )  
{  
    // Address of global i assigned to pointer variable:  
    static int *external_i = &i;  

    // i is redefined; global i no longer visible:   
    int i = 16;  

    // This a is visible only within the other function:   
    static int a = 2;  

    a += 2;  
    // Values printed are 16, 4, and 1:  
    printf_s( "%d\n%d\n%d\n", i, a, *external_i );  
}  

在此示例中,变量 i 是使用初始值 1 在外部级别定义的。In this example, the variable i is defined at the external level with initial value 1. extern 函数中的 main 声明用于声明对外部级别 i 的引用。An extern declaration in the main function is used to declare a reference to the external-level i. 由于忽略了初始值设定项,因此默认情况下 static 变量 a 将初始化为 0。The static variable a is initialized to 0 by default, since the initializer is omitted. printf 的调用将输出值 1、0、0 和 0。The call to printf prints the values 1, 0, 0, and 0.

other 函数中,全局变量 i 的地址用于初始化 static 指针变量 external_iIn the other function, the address of the global variable i is used to initialize the static pointer variable external_i. 此用法之所以有效是因为全局变量具有 static 生存期,这意味着其地址在程序执行期间不会更改。This works because the global variable has static lifetime, meaning its address does not change during program execution. 接下来,变量 i 将重新定义为初始值为 16 的局部变量。Next, the variable i is redefined as a local variable with initial value 16. 此重新定义不会影响外部级别 i 的值(通过对局部变量使用该外部级别的名称来隐藏)。This redefinition does not affect the value of the external-level i, which is hidden by the use of its name for the local variable. 全局 i 的值现在只能在此块中通过指针 external_i 间接访问。The value of the global i is now accessible only indirectly within this block, through the pointer external_i. 将 auto 变量 i 的地址分配给指针的尝试无效,因为每次输入块时该地址可能都不一样。Attempting to assign the address of the auto variable i to a pointer does not work, since it may be different each time the block is entered. 变量 a 将声明为 static 变量并初始化为 2。The variable a is declared as a static variable and initialized to 2. a 不与 main 中的 a 冲突,因为内部级别的 static 变量仅在声明它们的块中可见。This a does not conflict with the a in main, since static variables at the internal level are visible only within the block in which they are declared.

变量 a 增加了 2,并给出结果 4。The variable a is increased by 2, giving 4 as the result. 如果在同一程序中再次调用 other 函数,则 a 的初始值将为 4。If the other function were called again in the same program, the initial value of a would be 4. 当程序退出然后重新输入声明内部 static 变量的块时,这些变量将保留其值。Internal static variables keep their values when the program exits and then reenters the block in which they are declared.

另请参阅See Also

内部级别声明的存储类说明符Storage-Class Specifiers for Internal-Level Declarations