enum

C++ Reference

enum
Syntax:
  enum name {name-list} var-list;

The enum keyword is used to create an enumerated type named name that consists of the elements in name-list. The var-list argument is optional, and can be used to create instances of the type along with the declaration. For example, the following code creates an enumerated type for colors:

   enum ColorT {red, orange, yellow, green, blue, indigo, violet};
   ...
   ColorT c1 = indigo;
   if( c1 == indigo ) {
     cout << "c1 is indigo" << endl;
   }            

In the above example, the effect of the enumeration is to introduce several new constants named red, orange, yellow, etc. By default, these constants are assigned consecutive integer values starting at zero. You can change the values of those constants, as shown by the next example:

   enum ColorT { red = 10, blue = 15, green };
   ...
   ColorT c = green;
   cout << "c is " << c << endl;

When executed, the above code will display the following output:

   c is 16

Note that the above examples will only work with C++ compilers. If you're working in regular C, you will need to specify the enum keyword whenever you create an instance of an enumerated type:

   enum ColorT { red = 10, blue = 15, green };
   ...
   enum ColorT c = green;   // note the aditional enum keyword
   printf( "c is %d\n", c );