HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 3 Data Types and Declarations

Enumeration

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

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.

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, a long 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-constants will have the same size as the type specified in the enumeration declaration.
char enum {a};  /* sizeof(a) returns 1. */
© Hewlett-Packard Development Company, L.P.