Grouse: What's the point of enum?

Tom Armistead toma at swsrv1.cirr.com
Fri Apr 19 13:01:31 AEST 1991


In article <gu1k13w164w at cellar.UUCP> rogue at cellar.UUCP (Rogue Winter) writes:
>When I wanted to start learning C, I expected enumerated types to be very 
>useful in combination with the increment and decrement operators.  I hoped to 
>be able to use printf(...%s...) to show the named value I'd given a constant.
>
>Neither experiment had the desired effect.  I'm sure you all know the results 
>of such operations involved.  The point is, if increment operators on 
>enumerated variables don't produce increments of the defined values in the 
>enum statement (and enumerated variables are capable of having values
>not included in the explicit declarations), why bother declarig values for 
>them?
>
>If the names given to enumerated values cannot be printed, why do they exist? 
>The only purpose I can see is that they become local symbolic constants.
>
>Forgive a young novice her screed, but this just don't seem kosher.
>
>Rogue Winter      : "How can you say I only protected people in South
>rogue at cellar.uucp : Philadelphia?  I protected people all over this city; it
>uunet!cellar!rogue: didn't matter if they were in South Philadelphia or
>Cellar 215/3369503: Northeast Philadelphia."  -- Frank Rizzo, 4/12/91

A few reasons for enums:

  When in a symbolic debugger, you can display an enum variables contents and
  get the text associated with the value, where a define only shows the value
  (unless you have a really smart debugger).

  When used in a switch statememt for some C compilers), will not let you
  have case elements that aren't part of the emumeration. (i.e. better
  type checking).

  I've used enum's to generate some pretty complex types where certain bit
  patterns made something belong to a certain class. After getting this set up,
  it was very easy to just insert a new element in the middle of the enum,
  where using defines would have caused me to do math for each element I
  added.

>From the Second Edition of K&R: page 39
  Enumerations provide a convenient way to associate constant values with
  names, an alternative to #define with the advantage that values can be
  generated for you. ...

Tom
-- 
Tom Armistead - Software Services - 2918 Dukeswood Dr. - Garland, Tx  75040
===========================================================================
toma at swsrv1.cirr.com                {egsner,letni,ozdaltx,void}!swsrv1!toma



More information about the Comp.lang.c mailing list