enum

An enumeration data type represents a set of values that you declare. You can define an enumeration data type and all variables that have that enumeration type in one statement, or you can separate the declaration of the enumeration data type from all variable definitions. The identifier associated with the data type (not an object) is called an enumeration tag.

identifier Names the data type (like the tag on a struct data type).
enumerator Provides the data type with a set of values.

E

Each enumerator constant in the list has its own identifier, and represents an integer value. The integer value of an enumerator can be set implicitly by the position of the enumerator within the list, or explicitly by assigning an integral_constant_expression value to that enumerator.

To conserve space, enumerations may be stored in spaces smaller than that of an int.

Enumeration Constants
When you define an enumeration data type, you specify a set of identifiers that the data type represents. Each identifier in this set is called an enumeration constant.

The value of the constant is determined in the following way:

  1. An equal sign (=) and a constant expression after the enumeration constant gives an explicit value to the constant. The identifier represents the value of the constant expression.
  2. If no explicit value is assigned, the leftmost constant in the list receives the value zero (0).
  3. Identifiers with no explicitly assigned values receive the integer value that is one greater than the value represented by the previous identifier.

Each enumeration constant has an integer value. Use an enumeration constant anywhere an integer constant is allowed.

Each enumeration constant must be unique within the scope in which the enumeration is defined. In the following example, the declarations of average on line 4 and of poor on line 5 cause compiler error messages:

 1   func()
 2   {
 3      enum score { poor, average, good };
 4      enum rating { below, average, above };
 5      int poor;
 6   }

 

Defining Enumeration Variables
An enumeration variable definition contains an optional storage class specifier, a type specifier, a declarator, and an optional initializer. The type specifier contains the keyword enum followed by the name of the enumeration data type. You must declare the enumeration data type before you can define a variable having that type.

The initializer for an enumeration variable contains the = symbol followed by an expression. The initializer expression must evaluate to an int value.

The first line of the following example declares the enumeration tag grain. The second line defines the variable g_food and gives variable g_food the initial value of barley (2).

enum grain { oats, wheat, barley, corn, rice };
enum grain g_food = barley;

The type specifier enum grain indicates that the value of g_food is a member of the enumerated data type grain.

 

Defining an Enumeration Type and Enumeration Objects in the Same Statement
You can define a type and a variable in one statement by using a declarator and an optional initializer after the type definition. To specify a storage class specifier for the variable, you must put the storage class specifier at the beginning of the declaration. For example:

register enum score { poor=1, average, good } rating = good;

This example is equivalent to the following two declarations:

enum score { poor=1, average, good };
register enum score rating = good;

Both examples define the enumeration data type score and the variable rating. rating has the storage class specifier register, the data type enum score, and the initial value 3 (or good).

Combining a data type definition with the definitions of all variables having that data type lets you leave the data type unnamed. For example:

enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,
      Saturday } weekday;

defines the variable weekday, which can be assigned any of the specified enumeration constants.



Identifiers
Constant Expressions


Examples of Enumerator Declaration and Use