What is ENUM

List types can be defined with. This includes days of the week or colors. Names are assigned to the elements of an enumeration type, although they are of course internally coded as numbers.

definition

The keyword is followed by the name of the enumeration type. The names belonging to this type are listed between the following curly brackets. The names are separated by commas.

The syntax graph for enum (grafenum) is used at this point in the book

The following example defines an enumeration type for colors:

enum color {RED, YELLOW, GREEN, BLUE};

In the further course of the program, variables of the color type can be defined. Elements of the enumeration type can be assigned to these variables.

Color traffic light; ... traffic light = RED;

Internally, the enumerated values ​​are numbered consecutively. So RED is 0, YELLOW is 1 and so on. This is why the enumeration constants can also be assigned to integer variables. Conversely, there will definitely be a warning.

Color traffic light; int i; ... i = RED; // no problem traffic light = 3; // gives a warning These defaults can also be specified differently when defining the enumeration. Example: enum color {RED = 2, YELLOW, GREEN = 7, BLUE};

That would result in RED being 2, YELLOW 3, GREEN 7, and BLUE being 8.