简单变量声明Simple Variable Declarations

简单变量的声明(直接声明符的最简单形式)指定变量的名称和类型。The declaration of a simple variable, the simplest form of a direct declarator, specifies the variable's name and type. 它还指定变量的存储类和数据类型。It also specifies the variable's storage class and data type.

变量声明需要存储类和/或类型。Storage classes or types (or both) are required on variable declarations. 非类型化变量(如 var;)会生成警告。Untyped variables (such as var;) generate warnings.

语法Syntax

declaratordeclarator:
pointer optpointer opt

direct-declaratordirect-declarator

direct-declaratordirect-declarator:
identifieridentifier

identifier:identifier:
nondigitnondigit

identifier nondigitidentifier nondigit

identifier digitidentifier digit

对于算术、结构、联合、枚举和 void 类型以及由 typedef 名称表示的类型,可以在声明中使用简单声明符,因为类型说明符提供了所有类型化信息。For arithmetic, structure, union, enumerations, and void types, and for types represented by typedef names, simple declarators can be used in a declaration since the type specifier supplies all the typing information. 指针、数组和函数类型需要更复杂的声明符。Pointer, array, and function types require more complicated declarators.

可以使用一系列由逗号 (,) 分隔的标识符来指定同一个声明中的多个变量。You can use a list of identifiers separated by commas (,) to specify several variables in the same declaration. 声明中定义的所有变量都具有相同的基类型。All variables defined in the declaration have the same base type. 例如:For example:

int x, y;        /* Declares two simple variables of type int */  
int const z = 1; /* Declares a constant value of type int */  

变量 xy 可以保留由特定实现的 int 类型定义的集中的任何值。The variables x and y can hold any value in the set defined by the int type for a particular implementation. 简单对象 z 将初始化为值 1 且不可修改。The simple object z is initialized to the value 1 and is not modifiable.

如果 z 的声明针对未初始化的静态变量或在文件范围内,则它将接收初始值 0,并且该值是不可修改的。If the declaration of z was for an uninitialized static variable or was at file scope, it would receive an initial value of 0, and that value would be unmodifiable.

unsigned long reply, flag; /* Declares two variables  
                              named reply and flag     */  

在此示例中,两个变量(replyflag)具有 unsigned long 类型并包含无符号的整数值。In this example, both the variables, reply and flag, have unsigned long type and hold unsigned integral values.

另请参阅See Also

声明符和变量声明Declarators and Variable Declarations