Enumerated Constants

Enumerated constants create a set of constants. For example, you can declare COLOR to be an enumeration; and you can define that there are five values for COLOR: RED, BLUE, GREEN, WHITE, and BLACK.

The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally a closing brace and a semicolon. Here's an example:

enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };

This statement performs two tasks:

  1. It makes COLOR the name of an enumeration, that is, a new type.

  2. It makes RED a symbolic constant with the value 0, BLUE a symbolic ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.