🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5202

Re: enums

#5202
From: Tom Karzes
Date: Wed, 20 Jul 1988 08:00
40 lines
1726 bytes
While on the subject of enums, here's something that's always bothered me.
If you define an enum with a long list of values, a natural thing to want
to do is determine the number of values in the enum (sort of like sizeof).

For example:

    enum tree {
        oak,
        elm,
        maple,
        birch,
        willow,
        cypress,
        spruce
    };

Now I'd like to define TREE_COUNT to be the number of trees.  So I have to
painfully count them, and write:

    #define TREE_COUNT 7

and hope that people correctly update this macro every time they add or
delete a tree.

Sure, I suppose in this case I could write:

    #define TREE_COUNT ((int) spruce + 1)

(This assumes that there aren't any trees which have been given explicit
values in the enum.)  But this is still a pain to maintain, since you
have to fix the macro every time the last tree changes.

Of course, in the presence of enum members which are given explicit values
(which also introduces the possibility of duplicate values), you may want
to know something more than just the number of members (e.g., the number
of distinct values, the minimum and maximum values, etc.).  However, for
most situations it would be sufficient to simply know the number of members
in the enum.

Does ANSI C provide a reasonable way to do this?
Technical Details
Message-ID: <469@m3.mfci.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!yale!mfci!karzes
References: <1608@dataio.Data-IO.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!yale!mfci!karzes
From: karzes@mfci.UUCP (Tom Karzes)
Newsgroups: comp.std.c,comp.lang.c
Subject: Re: enums
Message-ID: <469@m3.mfci.UUCP>
Date: 20 Jul 88 08:00:41 GMT
References: <1608@dataio.Data-IO.COM>
Sender: root@mfci.UUCP
Reply-To: karzes@mfci.UUCP (Tom Karzes)
Organization: Multiflow Computer Inc., Branford Ct. 06405
Lines: 40