CHAPTER 12

image

Enum

Enum is a user-defined type consisting of a fixed list of named constants. In the following example, the enumeration type is called color and contains three constants: RED, GREEN and BLUE.

enum color { RED, GREEN, BLUE };

The color type can be used to create variables that may hold one of these constant values. In C, the variable declaration must be preceded by enum, whereas this is optional in C++.

int main(void) {  enum color c = RED;}

Enum variables may also be declared when the enum is defined, by placing the variable names before the final semicolon. This position is known as the declarator list.

enum color { RED, GREEN, BLUE ...

Get C Quick Syntax Reference 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.