HP C/iX Reference Manual (31506-90011)

Chapter 3 39
Data Types and Declarations
Enumeration
Enumeration
The identifiers in an enumeration list are declared as constants.
Syntax
enum-specifier
::=
enum[ identifier] {enumerator-list}
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. Note that enum
constant names must be unique.
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.
enum color x, y[100];
In this example, the color enumeration tag declares two objects. The x object is a scalar
enum object, while y is an array of 100 enums.
An enumeration tag cannot be used before its enumerators are declared.