HP C A.06.05 Reference Manual
Data Types and Declarations
Enumeration
Chapter 352
Enumeration
The identifiers in an enumeration list are declared as constants.
Syntax
enum-specifier
::=
[
type-specifier
]
enum [ identifier ] {enumerator-list}
[
type-specifier
]
enum identifier
enumerator-list
::=
enumerator
enumerator-list , enumerator
enumerator
::=
enumeration-constant
enumeration-constant = constant-expression
enumeration-constant
::=
identifier
Description
The identifiers defined in the enumerator list are enumeration constants of type int. As
constants, they can appear wherever integer constants are expected. A specific integer value
is associated with an enumeration constant by following the constant with an equal sign (=)
and a constant expression. If you define the constants without using the equal sign, the first
constant will have the value of zero and the second will have the value of one, and so on. If an
enumerator is defined with the equal sign followed by a constant expression, that identifier
will take on the value specified by the expression. Subsequent identifiers appearing without
the equal sign will have values that increase by one for each constant. For example,
enum color {red, blue, green=5, violet};
defines red as 0, blue as 1, green as 5, and violet as 6.
Enumeration constants share the same name space as ordinary identifiers. They have the
same scope as the scope of the enumeration in which they are defined. You can also use the
int or long type specifier to indicate 4-byte enums, even though 4-byte enums are the default.
The identifier in the enum declaration behaves like the tags used in structure and union
declarations. If the tag has already been declared, you can use the tag as a reference to that
enumerated type later in the program.