

enum                        C Keyword                        enum




Declare a type and identifiers


An enum  declaration is a data type  whose syntax resembles those
of the struct and  union declarations.  It lets you enumerate the
legal value for a given variable.  For example,


        enum opinion {yes, maybe, no} GUESS;


declares type opinion can have  one of three values: yes, no, and
maybe.   It  also  declares the  variable  GUESS  to  be of  type
opinion.

As with  a struct or union declaration, the  tag (ooppiinniioonn in this
example) is  optional; if present,  it may be  used in subsequent
declarations.  For example, the statement


        register enum opinion *op;


declares a register pointer to an object of type opinion.

All enumerated identifiers  must be distinct from all other iden-
tifiers in the program.   The identifiers act as constants and be
used wherever constants are appropriate.

COHERENT assigns  values to the  identifiers from left  to right,
normally beginning with zero and increasing by one.  In the above
example, the values of  yes, no, and maybe are set, respectively,
to one,  two, and three.  The values often  are iinntts, although if
the range of values is small enough, the enum will be an unsigned
char.   If an  identifier in  the declaration  is followed  by an
equal sign  and a constant, the identifier  is assigned the given
value, and subsequent values increase by one from that value; for
example,


        enum opinion {yes=50, no, maybe} guess;


sets the values of the identifiers  yes, no, and maybe to 50, 51,
and 52, respectively.

***** See Also *****

C keywords







COHERENT Lexicon                                           Page 1


