Defining Enumeration Variables

An enumeration variable definition has the following form:

>>-+-------------------------+---------------------------------->
   '-storage_class_specifier-'
 
>--enum--enumeration_data_type_name--identifier----------------->
 
>--+-------------------------+---------------------------------><
   '-=--enumeration_constant-'
 
 

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

C++ The initializer for an enumeration variable contains the = symbol followed by an expression enumeration_constant. 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;

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

C++ The enum keyword is optional when declaring a variable with enumeration type. However, it is required when declaring the enumeration itself. For example, both statements declare a variable of enumeration type:

enum grain g_food = barley;
     grain cob_food = corn;

Related References

IBM Copyright 2003