Path: 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 Summary: Expires: Sender: Followup-To: Distribution: Keywords: 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};