Initialization of enumerations

The initializer for an enumeration variable contains the = symbol followed by an expression enumeration_constant.

C++ In C++, the initializer must have the same type as the associated enumeration type.

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

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

Related information