Enumerations

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 declare an enumeration type separately from the definition of variables of that type. The identifier associated with the data type (not an object) is called an enumeration tag.

In C, an enumeration has an implementation-defined integral type. This restriction does not apply to C++. In C++, an enumeration has a distinct type that does not have to be integral.

An enumeration type declaration contains the enum keyword followed by an optional identifier (the enumeration tag) and a brace-enclosed list of enumerators. Commas separate each enumerator.

The keyword enum, followed by the identifier, names the data type (like the tag on a struct data type). The list of enumerators provides the data type with a set of values.

In C, each enumerator represents an integer value. In C++, each enumerator represents a value that can be converted to an integral value.

To conserve space, enumerations may be stored in spaces smaller than that of an int. By default, the type of the enum variable is the size of the smallest integral type that can contain all enumerator values.

When you define an enumeration data type, you specify a set of identifiers that the data type represents. Each identifier in this set is 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.

In C, enumeration constants have type int.

In C++ , each enumeration constant has a value that can be promoted to a signed or unsigned integer value and a distinct type that does not have to be integral. Use an enumeration constant anywhere an integer constant is allowed, or for C++, anywhere a value of the enumeration type is allowed.

Each enumeration constant must be unique within the scope in which the enumeration is defined. It is possible to associate the same integer with two different enumeration constants.



Declarators
Initializers
Defining Enumeration Variables
Defining Enumeration Types and Objects
Syntax of an Enumeration