HP C A.06.05 Reference Manual

Data Types and Declarations
Enumeration
Chapter 3 53
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.
Examples
enum color {RED, GREEN, BLUE};
enum objectkind {triangle, square=5, circle}; /* circle == 6 */
Sized enum - HP C Extension
By default, the HP C compiler on HP 9000 systems allocates four bytes for all enumerated
variables. However, if you know that the range of values being assigned to an enum variable is
small, you can direct the compiler to allocate only one or two bytes by using the char or short
type specifier. If the range is large, you can direct the compiler to allocate eight bytes by using
the long long type specifier. You can also use the long type specifier to indicate 4-byte
enums, even though this is the default. For example:
long long enum bigger_enum {barge, yacht}; /* 8-byte enum type */
enum default_enum {ERR1, ERR2, ERR3, ERR4}; /* 4-byte enum type */
long enum big_enum {STO, ST1, ST2, ST3}; /* 4-byte enum type */
short enum small_enum {cats, dogs}; /* 2-byte enum type */
char enum tiny_enum {alpha, beta}; /* 1-byte enum type */
When mixed in expressions, enums behave exactly as their similarly sized type counterparts
do. That is, an enum behaves like an int,along enum acts like a long int, and a short enum
acts like a short int. You will, however, receive a warning message when you mix enum
variables with integer or floating-point types, or with differently typed enums.
The sizeof() function returns the actual storage allocated when called with
enum-specifier
.
NOTE
enumeration-constant
s will have the same size as the type specified in the
enumeration declaration.
char enum {a}; /* sizeof(a) returns 1. */