enums

jeff at rlgvax.UUCP jeff at rlgvax.UUCP
Tue Jul 5 10:13:00 AEST 1983


As understand it, the enum type in C is a adaptation of one of Pascal's
nicer features.  Its motivation is not to enable types to be packed
tighter than in ints, and definitely not to allow tricky combinations
of enums and ints in expressions.

Enums deliberately allow (or should allow) less flexibility than ints.
The idea here is the general idea behind strong typing--have the person
declare what something is, rather than how it is to be implemented, and
disallow all expressions which involve disparate types which happen to
have the same implementation.

The things enums should replace were (and almost always still are)
implemented as #define's.  The signals (from SIGNAL(2) and signal.h)
and the system call error numbers (INTRO(2) and error.h) are two prime
examples of things begging to be made enums.  Anyone trying arithmetic
with them, or comparing them to ints, longs or chars, signed or
unsigned, or for that matter, comparing them with others of the same
type except for == and !=, is probably making a mistake and deserves to
have a type cast forced on him.  The object of the game with enums is
to allow you less flexibility!  If you are going to complain that you
cannot add them to a pointer, you do not really want enums, and
probably either do not understand or believe in, or both, their
motivation, which is somewhat alien to C.

So after all this, you ask, do I use enums.  No, I don't.  I have to
port my code and many C compiler writers feel they need only obey the
K&R book, and do not implement them well.  For example, one compiler I
have heard of disallows enums in switch statements, making them next to
useless.  They come from the same set of identifiers as ordinary
identifiers, according to the "Recent Changes" document, which is sort
of like not allowing two different ints to take the value 5.  Enum
constants, like int constants, are values--just not numeric.  I may get
up the courage to try them again someday.

               Jeffrey Kegler
               CCI Office Systems Division (formerly RLG)
               ...{allegra,seismo,mcnc,lime,we13,brl-bmd}!rlgvax!jeff



More information about the Comp.lang.c mailing list