enums

T. William Wells bill at proxftl.UUCP
Thu Jul 21 21:08:21 AEST 1988


In article <469 at m3.mfci.UUCP> karzes at 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};



More information about the Comp.std.c mailing list