enum

enum [tag] {enum-list} [declarator];   // for definition of enumerated type

enum tag declarator;   // for declaration of variable of type tag

The enum keyword specifies an enumerated type.

An enumerated type is a user-defined type consisting of a set of named constants called enumerators. By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn’t have unique values. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined.

In C, you can use the enum keyword and the tag to declare variables of the enumerated type. In C++, you can use the tag alone.

In C++, enumerators defined within a class are accessible only to member functions of that class unless qualified with the class name (for example, class_name::enumerator). You can use the same syntax for explicit access to the type name (class_name::tag).

For related information, see class and struct.

Example

// Example of the enum keyword
enum Days               // Declare enum type Days
{
   saturday,            // saturday = 0 by default
   sunday = 0,          // sunday = 0 as well
   monday,              // monday = 1
   tuesday,             // tuesday = 2
   wednesday,           // etc.
   thursday,
   friday
} today;                // Variable today has type Days

int  tuesday;           // Error, redefinition of tuesday

enum Days yesterday;    // Legal in C and C++
Days tomorrow;          // Legal in C++ only

yesterday = monday;

int i = tuesday;        // Legal; i = 2
yesterday = 0;          // Error; no conversion
yesterday = (Days)0;    // Legal, but results undefined