Article View: comp.std.c
Article #5218Re: enums
From: T. William Wells
Date: Thu, 21 Jul 1988 11:08
Date: Thu, 21 Jul 1988 11:08
37 lines
1398 bytes
1398 bytes
In article <469@m3.mfci.UUCP> karzes@mfci.UUCP (Tom Karzes) writes:
) 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
)...
) Does ANSI C provide a reasonable way to do this?
ANSI is irrelevant. Do it like
enum tree {
oak, elm, maple, birch, willow, cypress, spruce,
tree_size};
#define TREE_COUNT ((int)tree_size)
unless you don't need the cast, in which case, just do this:
enum tree {
oak, elm, maple, birch, willow, cypress, spruce,
TREE_COUNT};
Technical Details
Message-ID:
<513@proxftl.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!umd5!uflorida!novavax!proxftl!billReferences:
<1608@dataio.Data-IO.COM> <469@m3.mfci.UUCP>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!umd5!uflorida!novavax!proxftl!bill From: bill@proxftl.UUCP (T. William Wells) Newsgroups: comp.std.c Subject: Re: enums Message-ID: <513@proxftl.UUCP> Date: 21 Jul 88 11:08:21 GMT References: <1608@dataio.Data-IO.COM> <469@m3.mfci.UUCP> Reply-To: bill@proxftl.UUCP (T. William Wells) Organization: Proximity Technology, Ft. Lauderdale Lines: 37