Enumerated Types

You can use the enumerated type to declare symbolic names to represent integer constants. By using the enum keyword, you can create a new “type” and specify the values it may have. (Actually, enum constants are type int, therefore 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 tag name, which allows you to use enum spectrum as a type name. The second declaration makes color a variable of that type. The identifiers within the braces ...

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