enum style

Silver gaynor at paul.rutgers.edu
Tue May 28 07:01:39 AEST 1991


bill at gothamcity.jsc.nasa.gov (Bill Shirley) writes:
> [Opinions on the overall style of the following enum example?  
> I'm mostly concerned with readability/maintainability/portability.]
>
> typedef enum {  NoState,
>         STATE_A, STATE_B, STATE_C, /* ... */
>                 NUM_STATES } State;

There are some drain-bamaged compilers out there that don't by default map the
first element to 0.  I recommend explicitly mapping the first element to the
desired integer, and, to avoid name clashes, prefix each of the elements with
the name of the type.  Mine tend to look like this:

    typedef enum {thing_min = 0,
                  thing_0 = thing_min,
                  thing_1,
                  thing_2,
                  ...,
                  thing_N,
                  thing_max = thing_N,
                  thing_lim}
            thing;

> NUM_STATES will be the correct value always?
Should be, unless you veer from the default ordering.  In your example, it
should have the value 4; in mine, N+1.

Regards, [Ag]



More information about the Comp.lang.c mailing list