Names specified here
Name Description Notes Source Availability
enum Introduces enumeration type L Keyword C89 C90 C95 C99 C11

An enumeration defines symbolic integer constants, using the keyword enum. The declaration:

enum { FORWARD, RIGHT, BACKWARD, LEFT };

…defines four symbolic constants for the integer values 0, 1, 2 and 3. They can be used in place of integer constants with the same values:

int t;
t = RIGHT;
if (t == BACKWARD) . . .

These symbols are not variables, so you can't assign to them. They are not even objects, and so don't have addresses:

LEFT = -10;       // error
void *p = &RIGHT; // error

Values may be associated with symbols explicitly:

enum { FORWARD, RIGHT = 5, BACKWARD, LEFT };

This defines the symbols to mean 0, 5, 6 and 7. Each implicitly associated symbol has a value one more than the previous (or zero if it is the first). The same value may be repeated.

The symbols above have values of an anonymous enumeration type which is compatible with char or an integer type, as chosen by the compiler. Alternatively, an explicit type name may be given:

enum turn { FORWARD, RIGHT, BACKWARD, LEFT };

This defines a new enumeration type called enum turn (turn is the type's tag), and objects of this type can be declared.

enum turn t;
t = RIGHT;
if (t == BACKWARD) . . .

As with many other constructs in C, the above enum declarations are actually variable declarations, just with an empty list of variables. You can declare variables when declaring these types:

// a variable of an anonymous enumeration type
enum { FORWARD, RIGHT, BACKWARD, LEFT } foo;

// declaring a new type and variable together
enum turn { FORWARD, RIGHT, BACKWARD, LEFT } foo;

You can, of course, define aliases for enumeration types too, using typedef:

// an alias for an otherwise anonymous type
typedef enum { FORWARD, RIGHT, BACKWARD, LEFT } turn_t;

typedef enum turn { FORWARD, RIGHT, BACKWARD, LEFT } turn_t;

Enumeration constants exist in the general namespace for identifiers, so it is illegal to define two constants with the same name. Enumeration tags exist in a separate namespace, one shared with tags for struct and union, so enum turn and turn are always distinct.

type-specifier
enum-specifier
enum-specifier
enum identifieropt { enumerator-list }
enum identifieropt { enumerator-list , }
since C99
enum identifier
enumerator-list
enumerator
enumerator-list , enumerator
enumerator
enumeration-constant
enumeration-constant = constant-expression
enumeration-constant
identifier

CHaR
Sitemap Supported
Site format updated 2024-06-05T22:37:07.391+0000
Data updated 1970-01-01T00:00:00.000+0000
Page updated 2022-06-17T21:43:05.000+0000