[comp.lang.c] Question on enumeration behavior

dougg@vice.TEK.COM (Doug Grant) (01/14/88)

I have observed that when I define an enumeration type, the first
enumeration constant receives the integer value 0, the next 1, etc.
(using the Berkeley C compiler and Microsoft C 5.0).
Is this behavior guaranteed by the ANSI standard?  I want to be sure
that if I use enumeration constants to index into an array, that
the results will be the same, regardless of which (ANSI compatible)
C compiler is used to compile my program.

Thanks for your assistance,

Doug Grant
dougg@vice.TEK.COM
...!tektronix!vice!dougg

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/14/88)

In article <2195@vice.TEK.COM> dougg@vice.TEK.COM (Doug Grant) writes:
>I have observed that when I define an enumeration type, the first
>enumeration constant receives the integer value 0, the next 1, etc.
>Is this behavior guaranteed by the ANSI standard?

Yes, and you can force any value to be used for the start of a
sequence of enumeration values by specifying it, as in:
	enum { red = 4, blue, green, yellow = 1 } color;
which gives blue and green the values 5 and 6 respectively.