An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them. In addition to providing a way of defining and grouping sets of integral constants, enumerations are useful for variables that have a small number of possible values.

You can declare an enumeration type separately from the definition of variables of that type, as described in Enumeration type definition and Enumeration variable declarations; or you can define an enumeration data type and all variables that have that type in one statement, as described in Enumeration type and variable definitions in a single statement.

Enumeration type definition

An enumeration type definition contains the enum keyword followed by an optional identifier (the enumeration tag) and a brace-enclosed list of enumerators. A comma separates each enumerator in the enumerator list. C only C99 allows a trailing comma between the last enumerator and the closing brace.C only

Enumeration definition syntax

1  enum
2?  tag_identifier
2  { + , enumerator }
2  ;

The tag_identifier gives a name to the enumeration type. If you do not provide a tag name, you must put all variable definitions that refer to the enumeration type within the declaration of the type, as described in Enumeration type and variable definitions in a single statement. Similarly, you cannot use a type qualifier with an enumeration definition; type qualifiers placed in front of the enum keyword can only apply to variables that are declared within the type definition.

Elaborated type specifier

Elaborated type specifier syntax

1  enum  tag_identifier x

The elaborated type specifier refers to a previously declared enumeration. The x is a variable that has the type tag_identifier.

The enum keyword can be used to refer to scoped or unscoped enumerations during variable declaration or definition. For example:
// a scoped enumeration
enum class color { red, white, black, yellow }; 

// an unscoped enumeration
enum letter {A, B, C, D};   

// valid, regular type name usage
color pic1 = color :: white;

// valid, elaborated type usage
enum color pic2 = color :: red; 

You cannot use enum class or enum struct in the elaborated type specifier. For example:
enum class color pic3 = color :: black;    // invalid
The elaborated type specifier for an unscoped enumeration is the same as that for a scoped enumeration. For example:
enum letter let1 = letter :: A;            // valid

Enumeration members

The list of enumeration members, or enumerators, provides the data type with a set of values.

Enumeration member declaration syntax

1  identifier?  = enumeration_constant

C only In C, an enumeration constant is of type int. If a constant expression is used as an initializer, the value of the expression cannot exceed the range of int (that is, INT_MIN to INT_MAX as defined in the header limits.h). Otherwise, the condition is tolerated, a diagnostic message is issued, but the value of the enumeration constant is undefined.C only

C++ only 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. You can use an enumeration constant anywhere an integer constant is allowed, or anywhere a value of the enumeration type is allowed.C++ only

The value of an enumeration 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 enumeration constant. The enumeration constant represents the value of the constant expression.
  2. If no explicit value is assigned to the first enumerator, then it takes the value 0 (zero).
  3. Enumeration constants with no explicitly assigned values receive the integer value that is one greater than the value represented by the previous enumeration constant.
The following data type declarations list oats, wheat, barley, corn, and rice as enumeration constants. The number under each constant shows the integer value.
enum grain { oats, wheat, barley, corn, rice };
   /*         0      1      2      3     4         */

enum grain { oats=1, wheat, barley, corn, rice };
   /*         1        2      3      4     5       */

enum grain { oats, wheat=10, barley, corn=20, rice };
   /*          0     10        11     20       21  */
It is possible to associate the same integer with two different enumeration constants. For example, the following definition is valid. The identifiers suspend and hold have the same integer value.
enum status { run, clear=5, suspend, resume, hold=6 };
   /*          0      5        6       7       6       */
Each enumeration constant must be unique within the scope in which the enumeration is defined. In the following example, the second declarations of average and poor cause compiler errors:
       enum score { poor, average, good };
       enum rating { below, average, above };
       int poor;

Enumeration variable declarations

You must declare the enumeration data type before you can define a variable having that type.

Enumeration variable declaration syntax

2.1 storage_class_specifier
2.1 type_qualifier
1  enum
1  tag_identifier  declarator
The tag_identifier indicates the previously-defined data type of the enumeration.

C++ only The keyword enum is optional in enumeration variable declarations.C++ only

Enumeration type and variable definitions in a single statement

You can define a type and a variable in one statement by using a declarator and an optional initializer after the variable 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;
C++ only C++ also lets you put the storage class immediately before the declarator list. For example:
enum score { poor=1, average, good } register rating = good;
C++ only
Either of these examples 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 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. However, you cannot declare any additional enumeration variables using this set of enumeration constants.