Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!apple!netcom!amdcad!dgcad!dg-rtp!webo!dg-webo!pds From: pds@lemming.webo.dg.com (Paul D. Smith) Newsgroups: comp.lang.c Subject: Re: Grouse: What's the point of enum? Message-ID: Date: 19 Apr 91 01:10:38 GMT References: Sender: usenet@webo.dg.com (Usenet Administration) Organization: NSDD/ONSD, Data General Corp., Westboro, MA Lines: 47 In-Reply-To: rogue@cellar.UUCP's message of 18 Apr 91 04:26:15 GMT [] On 18 Apr 91 04:26:15 GMT, rogue@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@lemming.webo.dg.com | | Data General Corp. | | | Network Systems Development Division | "Pretty Damn S..." | | Open Network Systems Development | | ------------------------------------------------------------------