An enumerated type is a distinct type whose value is a value of its underlying type (see below), which includes the values of explicitly named constants (enumeration constants).
Enumerated type is declared using the following enumeration specifier as the type specifier in the declaration grammar:
enum identifier(optional) { enumerator-list } |
where enumerator-list is a comma-separated list (with trailing comma permitted) (since C99) of enumerators, each of which has the form:
enumerator | (1) | |
enumerator = constant-expression | (2) |
where.
identifier, enumerator | - | identifiers that are introduced by this declaration |
constant-expression | - | integer constant expression whose value is representable as a value of type int |
As with struct or union, a declaration that introduced an enumerated type and one or more enumeration constants may also declare one or more objects of that type or type derived from it.
enum color_t {RED, GREEN, BLUE}, c = RED, *cp = &c; // introduces the type enum color_t // the integer constants RED, GREEN, BLUE // the object c of type enum color_t // the object cp of type pointer to enum color_t
Each enumerator that appears in the body of an enumeration specifier becomes an integer constant with type int
in the enclosing scope and can be used whenever integer constants are required (e.g. as a case label or as a non-VLA array size).
enum color_t { RED, GREEN, BLUE} r = RED; switch(r) { case RED : puts("red"); break; case GREEN : puts("green"); break; case BLUE : puts("blue"); break; }
If enumerator is followed by = constant-expression, its value is the value of that constant expression. If enumerator is not followed by = constant-expression, its value is the value one greater than the value of the previous enumerator in the same enumeration. The value of the first enumerator (if it does not use = constant-expression) is zero.
enum Foo { A, B, C=10, D, E=1, F, G=F+C}; //A=0, B=1, C=10, D=11, E=1, F=2, G=12
The identifier itself, if used, becomes the name of the enumerated type in the tags name space and requires the use of the keyword enum (unless typedef'd into the ordinary name space).
enum color_t { RED, GREEN, BLUE}; enum color_t r = RED; // OK // color_t x = GREEN: // Error: color_t is not in ordinary name space typedef enum color_t color; color x = GREEN; // OK
Each enumerated type is compatible with one of: char
, a signed integer type, or an unsigned integer type. It is implementation-defined which type is compatible with any given enumerated type, but whatever it is, it must be capable of representing all enumerator values of that enumeration.
Enumerated types are integer types, and as such can be used anywhere other integer types can, including in implicit conversions and arithmetic operators.
enum { ONE = 1, TWO } e; long n = ONE; // promotion double d = ONE; // conversion e = 1.2; // conversion, e is now ONE e = e + 1; // e is now TWO
Unlike struct or union, there are no forward-declared enums in C:
enum Color; // Error: no forward-declarations for enums in C enum Color { RED, GREEN, BLUE};
Enumerations permit the declaration of named constants in a more convenient and structured fashion than does #define
; they are visible in the debugger, obey scope rules, and participate in the type system.
#define TEN 10 struct S { int x : TEN; }; // OK
or.
enum { TEN = 10 }; struct S { int x : TEN; }; // also OK
enum
.
C++ documentation for enumeration declaration |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/language/enum