Grouse: What's the point of enum?
Paul D. Smith
pds at lemming.webo.dg.com
Fri Apr 19 11:10:38 AEST 1991
[] On 18 Apr 91 04:26:15 GMT, rogue at cellar.UUCP (Rogue Winter) said:
RW> If the names given to enumerated values cannot be printed, why do
RW> they exist? The only purpose I can see is that they become local
RW> symbolic constants.
They are basically just for the edification of the programmer. It is
a handy way to state that a variable of the enum type should contain
one of the enumerated values, and no others. With a regular
preprocessor constant there is nothing to indicate that the variable
can't contain other values, short of putting in a comment to that
effect. With enums the compiler could generate a warning.
Some compilers can pick the size of the integer in which the enum is
stored based on the size of the largest (or sometimes smallest)
number: thus if your enum values were only 1-5 you'd get a short or
even a char, but if your values contained something like 1000000 you
get an int or long (depending on your architecture). Usually this
is only useful to try to force all enums to be the native size of the
CPU (since this is the most efficient size), unless they absolutely
can't fit.
Also, most modern debuggers are quite good at noticing when something
is an enum and *do* in fact print the symbolic form (usually in
addition to the numeric form) during debugging. This makes it quite
handy from the programmer's point of view.
If you think about it, there is no way you could have enums be
printable in string form without introducing some new syntax into C:
how would the compiler know when to interpret the value of the
variable as an integer (enum) and when to interpret it as a string?
The compiler obviously can't recognize that you're passing the enum to
a printf with a "%s" argument, so what if you just wanted to pass the
value of the enum to a function? You'd need some new syntax to say
"use the stringified name of this enum" rather than the enum value.
All in all, enums are just syntactic sugar. But some syntactic sugar
can make your programming life much nicer, IMHO, and enums do this
quite well without getting in the way of the "spirit of C".
paul
-----
------------------------------------------------------------------
| Paul D. Smith | pds at lemming.webo.dg.com |
| Data General Corp. | |
| Network Systems Development Division | "Pretty Damn S..." |
| Open Network Systems Development | |
------------------------------------------------------------------
More information about the Comp.lang.c
mailing list