初始化标量类型Initializing Scalar Types

初始化标量类型时,assignment-expression 的值将赋给变量。When initializing scalar types, the value of the assignment-expression is assigned to the variable. 赋值的转换规则适用。The conversion rules for assignment apply. (有关转换规则的信息,请参阅类型转换。)(See Type Conversions for information on conversion rules.)

语法Syntax

declarationdeclaration:
declaration-specifiers init-declarator-list opt;declaration-specifiers init-declarator-list opt;

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

init-declarator-listinit-declarator-list:
init-declaratorinit-declarator

init-declarator-list , init-declaratorinit-declarator-list , init-declarator

init-declaratorinit-declarator:
declaratordeclarator

declarator = initializer /* 用于标量初始化 */declarator = initializer /* For scalar initialization */

initializerinitializer:
assignment-expressionassignment-expression

您可初始化任何类型的变量,前提是遵循下列规则:You can initialize variables of any type, provided that you obey the following rules:

  • 在文件范围级别声明的变量可初始化。Variables declared at the file-scope level can be initialized. 如果未显式初始化外部级别的变量,则默认情况下它将初始化为 0。If you do not explicitly initialize a variable at the external level, it is initialized to 0 by default.

  • 常量表达式可用于初始化使用 static storage-class-specifier 声明的任何全局变量。A constant expression can be used to initialize any global variable declared with the static storage-class-specifier. 程序开始执行时将初始化声明为 static 的变量。Variables declared to be static are initialized when program execution begins. 如果未显式初始化全局 static 变量,则默认情况下该变量将初始化为 0,并且具有指针类型的所有成员都将赋给 null 指针。If you do not explicitly initialize a global static variable, it is initialized to 0 by default, and every member that has pointer type is assigned a null pointer.

  • 使用 auto 或 register 存储类说明符声明的变量将在执行控制每次传递到声明它的块中时进行初始化。Variables declared with the auto or register storage-class specifier are initialized each time execution control passes to the block in which they are declared. 如果省略 auto 或 register 变量的声明中的初始值设定项,则变量的初始值是不确定的。If you omit an initializer from the declaration of an auto or register variable, the initial value of the variable is undefined. 对于自动值和寄存器值,初始值设定项未限制为常量;它可以是包含之前定义的值的任何表达式,甚至是函数调用。For automatic and register values, the initializer is not restricted to being a constant; it can be any expression involving previously defined values, even function calls.

  • 外部变量声明和所有 static 变量(无论外部还是内部)的初始值必须是常量表达式。The initial values for external variable declarations and for all static variables, whether external or internal, must be constant expressions. (有关详细信息,请参阅常量表达式。)由于任何外部声明的变量或静态变量的地址为常量,因此可将该地址用于初始化内部声明的 static 指针变量。(For more information, see Constant Expressions.) Since the address of any externally declared or static variable is constant, it can be used to initialize an internally declared static pointer variable. 但是,auto 变量的地址不能用作静态初始值设定项,因为它可能在每次执行块时都不同。However, the address of an auto variable cannot be used as a static initializer because it may be different for each execution of the block. 可使用常量或变量值来初始化 auto 和 register 变量。You can use either constant or variable values to initialize auto and register variables.

  • 如果标识符的声明具有块范围,并且标识符具有外部链接,则该声明不能具有初始化。If the declaration of an identifier has block scope, and the identifier has external linkage, the declaration cannot have an initialization.

示例Examples

下列示例阐释了初始化:The following examples illustrate initializations:

int x = 10;   

整数变量 x 将初始化为常量表达式 10The integer variable x is initialized to the constant expression 10.

register int *px = 0;  

指针 px 将初始化为 0,从而生成“null”指针。The pointer px is initialized to 0, producing a "null" pointer.

const int c = (3 * 1024);  

此示例使用常量表达式 (3 * 1024)c 初始化为因 const 关键字而无法修改的常量值。This example uses a constant expression (3 * 1024) to initialize c to a constant value that cannot be modified because of the const keyword.

int *b = &x;  

此语句使用另一变量 b 的地址初始化指针 xThis statement initializes the pointer b with the address of another variable, x.

int *const a = &z;  

指针 a 是使用名为 z 的变量的地址初始化的。The pointer a is initialized with the address of a variable named z. 但是,由于该指针被指定为 const ,因此变量 a 只能被初始化,而始终不能被修改。However, since it is specified to be a const, the variable a can only be initialized, never modified. 该指针始终指向同一位置。It always points to the same location.

int GLOBAL ;  

int function( void )  
{  
    int LOCAL ;  
    static int *lp = &LOCAL;   /* Illegal initialization */  
    static int *gp = &GLOBAL;  /* Legal initialization   */  
    register int *rp = &LOCAL; /* Legal initialization   */  
}  

全局变量 GLOBAL 是在外部级别声明的,因此它具有全局生存期。The global variable GLOBAL is declared at the external level, so it has global lifetime. 局部变量 LOCAL 具有 auto 存储类,并且在执行声明它的函数时只具有一个地址。The local variable LOCAL has auto storage class and only has an address during the execution of the function in which it is declared. 因此,不允许尝试使用 LOCAL 的地址初始化 static 指针变量 lpTherefore, attempting to initialize the static pointer variable lp with the address of LOCAL is not permitted. static 指针变量 gp 可初始化为 GLOBAL 的地址,因为该地址始终相同。The static pointer variable gp can be initialized to the address of GLOBAL because that address is always the same. 同样,*rp 可初始化,因为 rp 是局部变量,并且可具有非常量初始值设定项。Similarly, *rp can be initialized because rp is a local variable and can have a nonconstant initializer. 每次输入块时,LOCAL 都具有新地址,该地址之后将赋给 rpEach time the block is entered, LOCAL has a new address, which is then assigned to rp.

请参阅See Also

初始化Initialization