DekGenius.com
Team LiB   Previous Section   Next Section
enum keyword Declares enumerated type or elaborates an enumerated type name

enum-specifier ::= enum [identifier] { [enumerator-list] }
enumerator-list ::= enumerator-defn | enumerator-list , enumerator-defn
enumerator-defn ::= enumerator | enumerator = constant-expr
enumerator ::= identifier
elaborated-type-specifier := enum [::] [nested-name ::] identifier

The enum keyword declares a new enumerated type (as an enum-specifier) or names an existing enumerated type (in an elaborated-type-specifier). An enumerated type is an integral type that defines a set of named constants (the enumerator-list). Each enumerator is an identifier optionally followed by a value (an equal sign and a constant expression of integral or enumerated type). Without an explicit value, the value of an enumerator is one more than the value of the preceding enumerator. The implicit value of the first enumerator is 0.

figs/acorn.gif

Every enumerated type is stored as an integral type. The size of the integer is implementation-defined but is large enough to hold all the enumerated values. The valid values of an enumeration include the declared enumerators and every value that can be stored in the same number of bits, even if those values are not named as enumerators.

Example

enum logical { no, maybe, yes };
bool logical_to_bool(enum logical x) // Redundant enum
{
  return x != no;
}

See Also

expression, identifier, type, Chapter 2

    Team LiB   Previous Section   Next Section