声明概述Overview of Declarations

“声明”指定一组标识符的解释和特性。A "declaration" specifies the interpretation and attributes of a set of identifiers. 还将导致针对标识符命名的对象或函数保留存储的声明将称为“定义”。A declaration that also causes storage to be reserved for the object or function named by the identifier is called a "definition." 用于变量、函数和类型的 C 声明都具有以下语法:C declarations for variables, functions, and types have this syntax:

语法Syntax

declarationdeclaration:
declaration-specifiers attribute-seqoptinit-declarator-listopt;declaration-specifiers attribute-seqoptinit-declarator-listopt;

/* attribute-seqopt 是 Microsoft 专用的 //* *attribute-seqopt is Microsoft specific */

declaration-specifiersdeclaration-specifiers:
storage-class-specifier declaration-specifiersoptstorage-class-specifier declaration-specifiersopt

type-specifier declaration-specifiersopttype-specifier declaration-specifiersopt

type-qualifier declaration-specifiersopttype-qualifier declaration-specifiersopt

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

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

init-declaratorinit-declarator:
declaratordeclarator

declarator = initializerdeclarator = initializer

备注

用于 declaration 的这种语法在以下部分中不重复。This syntax for declaration is not repeated in the following sections. 以下部分中的语法通常以 declarator 非终止符开头。Syntax in the following sections usually begins with the declarator nonterminal.

init-declarator-list 中的声明包含已命名的标识符;init 是初始值设定项的缩写。The declarations in the init-declarator-list contain the identifiers being named; init is an abbreviation for initializer. init-declarator-list 是逗号分隔的声明符序列,其中每个都可具有附加类型信息和/或初始值设定项。The init-declarator-list is a comma-separated sequence of declarators, each of which can have additional type information, or an initializer, or both. declarator 包含要声明的标识符(如果有)。The declarator contains the identifiers, if any, being declared. declaration-specifiers 非终止符由一列类型和存储类说明符组成,这些说明符指示链接、存储持续时间,并且至少可以指示声明符表示的部分实体类型。The declaration-specifiers nonterminal consists of a sequence of type and storage-class specifiers that indicate the linkage, storage duration, and at least part of the type of the entities that the declarators denote. 因此,声明由存储类说明符、类型说明符、类型限定符、声明符以及初始值的某种组合构成。Therefore, declarations are made up of some combination of storage-class specifiers, type specifiers, type qualifiers, declarators, and initializers.

声明可以包含 attribute-seq 中列出的一个或多个可选特性;seq 是序列的缩写。Declarations can contain one or more of the optional attributes listed in attribute-seq; seq is an abbreviation for sequence. 这些特定于 Microsoft 的特性会执行各种功能(本书中将进行详细介绍)。These Microsoft-specific attributes perform a variety of functions, which are discussed in detail throughout this book.

在变量声明的常规形式中,type-specifier 提供了变量的数据类型。In the general form of a variable declaration, type-specifier gives the data type of the variable. 当类型由 const 或 volatile 修改时,type-specifier 可以是复合形式。The type-specifier can be a compound, as when the type is modified by const or volatile. declarator 可为变量命名,并可能将其修改以用于声明数组类型或指针类型。The declarator gives the name of the variable, possibly modified to declare an array or a pointer type. 例如,For example,

int const *fp;  

将名为 fp 的变量声明为一个指向不可修改的 (const) int 值的指针。declares a variable named fp as a pointer to a nonmodifiable (const) int value. 可以使用多个声明符(用逗号分隔)来定义声明中的多个变量。You can define more than one variable in a declaration by using multiple declarators, separated by commas.

声明必须至少具有一个声明符,或者其类型说明符必须声明一个结构标记、联合标记或枚举的成员。A declaration must have at least one declarator, or its type specifier must declare a structure tag, union tag, or members of an enumeration. 声明符提供有关标识符的所有剩余信息。Declarators provide any remaining information about an identifier. 声明符是一个标识符,可使用方括号 ([ ])、星号 (*) 或括号 ( ( ) ) 对其进行修改,以分别声明数组、指针或函数类型。A declarator is an identifier that can be modified with brackets ([ ]), asterisks (\), or parentheses ( **( )* ) to declare an array, pointer, or function type, respectively. 在声明简单变量(例如字符、整数和浮点项)或简单变量的结构和联合时,declarator 只是一个标识符。When you declare simple variables (such as character, integer, and floating-point items), or structures and unions of simple variables, the declarator is just an identifier. 有关声明符的详细信息,请参阅声明符和变量声明For more information on declarators, see Declarators and Variable Declarations.

所有定义都为隐式声明,但并非所有声明都为定义声明。All definitions are implicitly declarations, but not all declarations are definitions. 例如,以 extern 存储类说明符开头的变量声明是“引用”声明而非“定义”声明。For example, variable declarations that begin with the extern storage-class specifier are "referencing," rather than "defining" declarations. 如果在定义外部变量之前引用它,或者在使用该变量的文件之外的其他源文件中定义它,则 extern 声明是必需的。If an external variable is to be referred to before it is defined, or if it is defined in another source file from the one where it is used, an extern declaration is necessary. 存储无法由“引用”声明分配,也不能在声明中初始化变量。Storage is not allocated by "referencing" declarations, nor can variables be initialized in declarations.

变量声明中需要存储类或类型(或者这两者)。A storage class or a type (or both) is required in variable declarations. 除了 __declspec,声明中只允许包含一个存储类说明符,并且并非每一个上下文中都允许存在所有存储类说明符。Except for __declspec, only one storage-class specifier is allowed in a declaration and not all storage-class specifiers are permitted in every context. __declspec 存储类可以与其他存储类说明符一起使用,并且允许多次一起使用。The __declspec storage class is allowed with other storage-class specifiers, and it is allowed more than once. 声明的存储类说明符会影响存储和初始化声明的项的方式,还会影响程序中的哪些部分可以引用该项。The storage-class specifier of a declaration affects how the declared item is stored and initialized, and which parts of a program can reference the item.

C 中定义的 storage-class-specifier 终止符包括 auto、extern、register、static 以及 typedefThe storage-class-specifier terminals defined in C include auto, extern, register, static, and typedef. 此外,Microsoft C 还包括 storage-class-specifier 终止符 __declspecIn addition, Microsoft C includes the storage-class-specifier terminal __declspec. 存储类中将讨论除 typedef__declspec 之外的所有 storage-class-specifier 终止符。All storage-class-specifier terminals except typedef and __declspec are discussed in Storage Classes. 有关 typedef 的信息,请参阅 Typedef 声明See Typedef Declarations for information about typedef. 有关 __declspec 的信息,请参阅扩展的存储类特性See Extended Storage-Class Attributes for information about __declspec.

源程序中声明的位置以及变量的其他声明存在与否是确定变量生存期的重要因素。The location of the declaration within the source program and the presence or absence of other declarations of the variable are important factors in determining the lifetime of variables. 可以有多个重新声明,但只能有一个定义。There can be multiple redeclarations but only one definition. 但是,定义可以出现在多个翻译单元中。However, a definition can appear in more than one translation unit. 对于带内部链接的对象,此规则可分别应用到每个翻译单元,因为内部链接的对象对翻译单元是唯一的。For objects with internal linkage, this rule applies separately to each translation unit, because internally linked objects are unique to a translation unit. 对于带外部链接的对象,此规则适用于整个程序。For objects with external linkage, this rule applies to the entire program. 有关可见性的详细信息,请参阅生存期、范围、可见性和链接See Lifetime, Scope, Visibility, and Linkage for more information about visibility.

类型说明符提供了一些有关标识符的数据类型的信息。Type specifiers provide some information about the data types of identifiers. 默认类型说明符是 intThe default type specifier is int. 有关详细信息,请参阅类型说明符For more information, see Type Specifiers. 类型说明符还可以定义类型标记、结构和联合组件名称以及枚举常量。Type specifiers can also define type tags, structure and union component names, and enumeration constants. 有关详细信息,请参阅枚举声明结构声明联合声明For more information see Enumeration Declarations, Structure Declarations, and Union Declarations.

type-qualifier 终止符有两种:const 和 volatileThere are two type-qualifier terminals: const and volatile. 这些限定符指定仅在通过左值访问该类型的对象时才相关的类型的其他属性。These qualifiers specify additional properties of types that are relevant only when accessing objects of that type through l-values. 有关 const 和 volatile 的详细信息,请参阅类型限定符For more information on const and volatile, see Type Qualifiers. 有关左值的定义,请参阅左值和右值表达式For a definition of l-values, see L-Value and R-Value Expressions.

另请参阅See Also

C 语言语法摘要 C Language Syntax Summary
声明和类型 Declarations and Types
声明摘要Summary of Declarations