Use of enumerations.

merlyn at sequent.UUCP merlyn at sequent.UUCP
Thu Apr 19 03:01:19 AEST 1984


ps at celerity.UUCP says:

> I have encountered a problem using enumerations with the 4.2bsd C compiler.
> The System 5 C Programming Guide states that the current language treats
> enumeration variables and constants as being of *int* type. There are
> apparently no special constructs in the language for incrementing and
> decrementing enumeration variables, and declaring arrays indexed by
> enumeration types. ...

and then goes on to demonstrate that the compiler chokes when attempting
to use enums like Pascal enumerated types.

The main problem here is the assumption that "enum"s are like Pascal's
enumerated types.  They aren't.  They're a funny kluge that keeps you from
having to ALWAYS do "#define FOO 1", "#define BAR 2", etc. to get a
distinguished set of constants.  They don't operate much different than
that, in fact.

The main problem with producing the things you asked about is that an enum
type (unlike the similar Pascal construct) is not necessarily dense.
For example, for the declaration:

	enum weekday {
		sun, mon, tue, wed = 100, thu, fri, sat
	} today, tomorrow;

what would "today++" or "tomorrow = today + 1" mean if "today" was "tue"???
Also, how many elements are there in an array indexed by "weekday"?
As you can quickly see, you don't really get much more support with enums
than the (roughly) equivalent:

	typedef int weekday;
	#define sun 1		/* or is it 0? can't remember now! */
	#define mon 2
	#define tue 3
	#define wed 100
	#define thu 101
	#define fri 102
	#define sat 103

except that you don't have to say all those "#define"s and it's a bit
easier to add and delete items.

Comments?

Randal L. ("(null)") Schwartz, esq. (merlyn at sequent.UUCP)
	(Official legendary sorcerer of the 1984 Summer Olympics)
Sequent Computer Systems, Inc. (503)626-5700
UUCP: ...!XXX!sequent!merlyn where XXX is one of:
	cdi decwrl nsc ogcvax pur-ee rocks34 shell unisoft vax135 verdix



More information about the Comp.lang.c mailing list