O'Reilly logo

Just Enough C/C++ Programming by Guy W. Lecky-Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Enumerated Types

Another kind of type, known as the enumerated type, allows you to create sets of values that are referenced by name, but that enumerate to an indexed position within that set. For example, you might like to create a user-defined type to store days of the week:

enum week_days {
Mon = 0, Tue, Wed, Thu, Fri, Sat, Sun
};

In this example, the compiler will assign the integer values 0 through 6 to the days of the week, in the order that they are listed. The convenience in programming terms is that the user-friendly word Mon can be used in the program, rather than the value 0.

So, you might write code such as:

week_days DayOfWeek;

This declares a variable DayOfWeek as being of the appropriate type. You can assign a value to the variable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required