'C' enum syntax problem

Gregory Kemnitz kemnitz at mitisft.Convergent.COM
Tue Apr 25 15:03:45 AEST 1989


In article <1095 at cvbnet2.UUCP>, krishna at xenon.uucp (Temporary) writes:
> I seem to have some problem in using the enum construct on Sun-3
> m/c.
> The following declarations generate an error.
> 
>    typedef enum WEEK {
>      MONDAY, TUESDAY, WEDNESDAY,
>      THURSDAY, FRIDAY,
>      SATURDAY, SUNDAY
>    } ;
>    typedef enum WEEK_END {
>      SATURDAY, SUNDAY
>    } ;
> 
> The error messages are
> 	redeclaration of SATURDAY
> 	redeclaration of SUNDAY
> 
> Does it mean that subsets of an enum cannot be defined ?
> Is this a bug in my C Compiler ?

Lots of C compilers just turn enums into things that are essentially
#defines (and barf on them when the symbol is reused). 
Another thing that won't work is

    typedef enum WEEK {
      MONDAY, TUESDAY, WEDNESDAY,
      THURSDAY, FRIDAY,
      SATURDAY, SUNDAY
    } ;

    ...
     
    WEEK workweek;

    switch (workweek) {
	 case MONDAY: ...
	 case TUESDAY: ...

	 ....

Lots of compilers will barf in the switch(), unless you use this

    switch ((int) workweek) {

which sucks.  

> Is there a work around ?

The easiest and most elegant workaround (and it always seems to work) is

#define MONDAY 0
#define TUESDAY 1
...

typedef short WEEK
typedef short WEEKEND

Then switches, etc will work fine.

				Greg Kemnitz



More information about the Comp.lang.c mailing list