Enumerated Types

You can use the enumerated type to declare symbolic names to represent integer constants. It is a common C extension that has been incorporated into the ANSI C standard. By using the enum keyword, you can create a new "type" and specify the values it may have. (Actually, enum constants are type int, so they can be used wherever you would use an int.) The purpose of enumerated types is to enhance the readability of a program. The syntax is similar to that used for structures. For example, you can make these declarations:

enum spectrum {red, orange, yellow, green, blue, violet};
enum spectrum color;

The first declaration establishes spectrum as a type name, and the second declaration makes color a variable of that type. The ...

Get C Primer Plus®, 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.