[comp.lang.c] How many values in an enum

dave@motto.UUCP (David Brown) (03/27/90)

In article <1425@polari.UUCP> blm@6sceng.UUCP (Brian Matthews) writes:
>ANSI enums, on the other hand, are
>little more than fancy #defines (although you can: a - insert new ones in the
>middle without renumbering the others by hand and b - have the compiler tell
>you how many there are.)

How do you get the compiler to tell you how many values an enumerated
type has?  Does this just refer to defining a final value, like

enum days_of_week = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
		     THURSDAY, FRIDAY, SATURDAY, N_DAYS_OF_WEEK };

or is there something more subtle?

 -----------------------------------------------------------------------------
|  David C. Brown	    |  uunet!mnetor!motto!dave			      |
|  Motorola Canada, Ltd.    |  416-499-1441 ext 3708			      |
|  Communications Division  |  "Of course I'm above average!  Who isn't?"     |
 -----------------------------------------------------------------------------

6sigma2@polari.UUCP (Brian Matthews) (03/30/90)

In article <96@motto.UUCP> dave@motto.UUCP (David Brown) writes:
|In article <1425@polari.UUCP> blm@6sceng.UUCP (Brian Matthews) writes:
|>ANSI enums, on the other hand, are
|>little more than fancy #defines (although you can: a - insert new ones in the
|>middle without renumbering the others by hand and b - have the compiler tell
|>you how many there are.)
|How do you get the compiler to tell you how many values an enumerated
|type has?  Does this just refer to defining a final value, like
|enum days_of_week = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|		     THURSDAY, FRIDAY, SATURDAY, N_DAYS_OF_WEEK };

That's all I meant.  If there is something more subtle, I don't know
about it.
-- 
Brian L. Matthews	blm@6sceng.UUCP

robert@cs.arizona.edu (Robert J. Drabek) (03/30/90)

> |How do you get the compiler to tell you how many values an enumerated
> |type has?  Does this just refer to defining a final value, like
> |enum days_of_week = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
> |		     THURSDAY, FRIDAY, SATURDAY, N_DAYS_OF_WEEK };


Or:
   (SATURDAY - SUNDAY + 1)
-- 
Robert J. Drabek                            robert@cs.Arizona.EDU
Department of Computer Science              uunet!arizona!robert
The University of Arizona                   602 621 4326
Tucson, AZ  85721

exspes@bath.ac.uk (P E Smee) (04/09/90)

In article <205@caslon.cs.arizona.edu> dave@cs.arizona.edu (David P. Schaumann) writes:
>I think the practice of giving enum symbols explicit values as above is
>poor programming practice.  If you want a symbol with a specific value,
>you should use either #define first 2 or int first = 2, depending on what
>suits your context.  If you want a group of ordered symbols, use enum.
>
>It adds no
>functionality or expressiveness to the language, and opens up the door for
>untold misunderstandings.

Naw, it's a handy shorthand when you need a set of symbols (maybe
ordered and maybe not) which have distinct values, and where you DO
care about some of the values -- often for historical reasons.  The
fact that they become an indentifiable 'weak type' can help error
checking if you have a good (IMHO) lint -- and I do wish C itself
handled enums as strong types.  Lot quicker than having to #define AND
#typedef.

(In fact, in principle I see little philosophical difference between
allowing 'forced' values in enums, and allowing declaration of arrays
with a non-0 lower bound.  Three things arise from this.  First, I
agree with you in that I wish I could declare arrays which ranged over
the portion of space that I am interested in, rather than always having
to manually (or macro-ally) handle any offset myself.  Second, I'm a bit
surprised that the C-standard authors didn't include them.  And third,
I'm a bit surprised that you don't see a use for forced-value enums.)

-- 
Paul Smee, Computing Service, University of Bristol, Bristol BS8 1UD, UK
 P.Smee@bristol.ac.uk - ..!uunet!ukc!bsmail!p.smee - Tel +44 272 303132

jharkins@sagpd1.UUCP (Jim Harkins) (04/10/90)

In article <205@caslon.cs.arizona.edu> dave@cs.arizona.edu (David P. Schaumann) writes:
-In article <5152@rtech.rtech.com>, mikes@rtech.UUCP (Mike Schilling) writes:
-> From article <7152@uhccux.uhcc.hawaii.edu>, by lupton@uhccux.uhcc.hawaii.edu (Robert Lupton):
->
->	[ discussion about # elements in an enum deleted ]
-> 
-> 	enum messy = {first=2, second, last=12};
-> 
-
-IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO IMHO
-
-I think the practice of giving enum symbols explicit values as above is
-poor programming practice.  If you want a symbol with a specific value,
-you should use either #define first 2 or int first = 2, depending on what
-suits your context.

Oh, I don't know.  Supposing you want to talk to a hardware register.

	enum bit_banger = {foo = 1, bar = 2, baz = 4, last = 0x8000};

Me?  I don't use enums because when I started with C some compilers supported
them, most didn't, and a few even supported them correctly.  Now that they're
in the standard (they are in the standard, aren't they?  I still don't have
K&R 2) I may start using them.  When I do use them I'd like to be able to
do the above, that seems like a real useful feature.


-- 
jim		jharkins@sagpd1

We are all aware of the high cost of alcohol abuse.  To help solve this problem
take this signature to your local liquor store for $1.00 off your next purchase.