C 枚举声明C Enumeration Declarations

枚举由一组命名整数常量构成。An enumeration consists of a set of named integer constants. 枚举类型声明提供(可选)枚举标记的名称,并定义命名整数标识符集(称为“枚举集”、“枚举器常量”、“枚举器”或“成员”)。An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the "enumeration set," "enumerator constants," "enumerators," or "members"). 具有枚举类型的变量存储该类型所定义的枚举集的值之一。A variable with enumeration type stores one of the values of the enumeration set defined by that type.

enum 类型的变量可用于索引表达式中,并且可用作所有算术和关系运算符的操作数。Variables of enum type can be used in indexing expressions and as operands of all arithmetic and relational operators. 枚举提供了 #define 预处理器指令的替代方法,带来的好处是可为您生成值并遵循一般范围规则。Enumerations provide an alternative to the #define preprocessor directive with the advantages that the values can be generated for you and obey normal scoping rules.

在 ANSI C 中,定义枚举器常量值的表达式始终具有 int 类型;因此,与枚举变量关联的存储是单个 int 值所需的存储。In ANSI C, the expressions that define the value of an enumerator constant always have int type; thus, the storage associated with an enumeration variable is the storage required for a single int value. 可以在 C 语言允许整数表达式的任意位置使用枚举常量或枚举类型的值。An enumeration constant or a value of enumerated type can be used anywhere the C language permits an integer expression.

语法Syntax

enum-specifierenum-specifier:
enum identifier opt{ enumerator-list }enum identifier opt{ enumerator-list }

enum identifierenum identifier

可选的 identifier 命名由 enumerator-list 定义的枚举类型。The optional identifier names the enumeration type defined by enumerator-list. 此标识符通常称为列表指定的枚举的“标记”。This identifier is often called the "tag" of the enumeration specified by the list. 该形式的类型说明符A type specifier of the form


enum  
identifier  
{  
enumerator-list  
}  

将 identifier 声明为由 enumerator-list 非终止符指定的枚举的标记。declares identifier to be the tag of the enumeration specified by the enumerator-list nonterminal. enumerator-list 定义“枚举器内容”。The enumerator-list defines the "enumerator content." 下面详细介绍了 enumerator-list。The enumerator-list is described in detail below.

如果标记的声明可见,则后续使用标记但忽略 enumerator-list 的声明将指定之前声明的枚举的类型。If the declaration of a tag is visible, subsequent declarations that use the tag but omit enumerator-list specify the previously declared enumerated type. 标记必须引用定义的枚举类型,并且该枚举类型必须在当前范围内。The tag must refer to a defined enumeration type, and that enumeration type must be in current scope. 由于在其他位置定义枚举类型,因此 enumerator-list 不会出现在此声明中。Since the enumeration type is defined elsewhere, the enumerator-list does not appear in this declaration. 在定义枚举类型之前,派生自枚举类型的枚举和 typedef 声明的类型的声明可以使用枚举标记。Declarations of types derived from enumerations and typedef declarations for enumeration types can use the enumeration tag before the enumeration type is defined.

语法Syntax

enumerator-listenumerator-list:
enumeratorenumerator

enumerator-list , enumeratorenumerator-list , enumerator

enumeratorenumerator:
enumeration-constantenumeration-constant

enumeration-constant = constant-expressionenumeration-constant = constant-expression

enumeration-constantenumeration-constant:
identifieridentifier

enumeration-list 中的每个 enumeration-constant 命名一个枚举集的值。Each enumeration-constant in an enumeration-list names a value of the enumeration set. 默认情况下,第一个 enumeration-constant 与值 0 相关联。By default, the first enumeration-constant is associated with the value 0. 列表中的下一个 enumeration-constant 与 ( constant-expression + 1 ) 的值相关联,除非显式将其与另一个值相关联。The next enumeration-constant in the list is associated with the value of ( constant-expression + 1 ), unless you explicitly associate it with another value. enumeration-constant 的名称与其值等效。The name of an enumeration-constant is equivalent to its value.

可以使用 enumeration-constant = constant-expression 替代值的默认序列。You can use enumeration-constant = constant-expression to override the default sequence of values. 因此,如果 enumeration-constant = constant-expression 出现在 enumerator-list 中,则将 enumeration-constant 与由 constant-expression 给定的值相关联。Thus, if enumeration-constant = constant-expression appears in the enumerator-list, the enumeration-constant is associated with the value given by constant-expression. constant-expression 必须具有 int 类型且可为负。The constant-expression must have int type and can be negative.

下面的规则适用于枚举集的成员:The following rules apply to the members of an enumeration set:

  • 枚举集可以包含重复的常量值。An enumeration set can contain duplicate constant values. 例如,可以将值 0 与两个不同的标识符关联,二者可能在同一集中命名为 nullzeroFor example, you could associate the value 0 with two different identifiers, perhaps named null and zero, in the same set.

  • 枚举列表中的标识符必须与同一范围中具有相同可见性的其他标识符不同,包括普通变量名和其他枚举列表中的标识符。The identifiers in the enumeration list must be distinct from other identifiers in the same scope with the same visibility, including ordinary variable names and identifiers in other enumeration lists.

  • 枚举标记遵循一般范围规则。Enumeration tags obey the normal scoping rules. 它们必须不同于具有相同可见性的其他枚举、结构和联合标记。They must be distinct from other enumeration, structure, and union tags with the same visibility.

示例Examples

这些示例阐释枚举声明:These examples illustrate enumeration declarations:

enum DAY            /* Defines an enumeration type    */  
{  
    saturday,       /* Names day and declares a       */  
    sunday = 0,     /* variable named workday with    */   
    monday,         /* that type                      */  
    tuesday,  
    wednesday,      /* wednesday is associated with 3 */  
    thursday,  
    friday  
} workday;  

默认情况下,值 0 与 saturday 关联。The value 0 is associated with saturday by default. 标识符 sunday 将显式设置为 0。The identifier sunday is explicitly set to 0. 默认情况下,将为剩余标识符提供从 1 到 5 的值。The remaining identifiers are given the values 1 through 5 by default.

在此示例中,将集 DAY 中的值赋给变量 todayIn this example, a value from the set DAY is assigned to the variable today.

enum DAY today = wednesday;  

请注意,可使用枚举常量的名称进行赋值。Note that the name of the enumeration constant is used to assign the value. 由于之前声明了 DAY 枚举类型,因此仅枚举标记 DAY 是必需的。Since the DAY enumeration type was previously declared, only the enumeration tag DAY is necessary.

若要显式将整数值赋给枚举数据类型的变量,请使用类型转换:To explicitly assign an integer value to a variable of an enumerated data type, use a type cast:

workday = ( enum DAY ) ( day_value - 1 );  

建议在 C 中进行此转换,但它不是必需的。This cast is recommended in C but is not required.

enum BOOLEAN  /* Declares an enumeration data type called BOOLEAN */  
{  
    false,     /* false = 0, true = 1 */  
    true   
};   

enum BOOLEAN end_flag, match_flag; /* Two variables of type BOOLEAN */  

还可将此声明指定为This declaration can also be specified as

enum BOOLEAN { false, true } end_flag, match_flag;\  

或指定为or as

enum BOOLEAN { false, true } end_flag;  
enum BOOLEAN match_flag;  

使用上述变量的示例可能类似于以下内容:An example that uses these variables might look like this:

if ( match_flag == false )  
    {  
     .  
     .   /* statement */   
     .  
    }  
    end_flag = true;  

还可以声明未命名的枚举器数据类型。Unnamed enumerator data types can also be declared. 忽略数据类型的名称,但可以声明变量。The name of the data type is omitted, but variables can be declared. 变量 response 是已定义的类型的变量:The variable response is a variable of the type defined:

enum { yes, no } response;  

另请参阅See Also

枚举Enumerations