Are enums safe to use in portable code?

Chris Torek torek at elf.ee.lbl.gov
Thu Feb 14 19:25:48 AEST 1991


In article <128 at tdatirv.UUCP> sarima at tdatirv.UUCP (Stanley Friesen) writes:
>According to 3.5.2.2 (Semantics):
>"The identifiers in an enumerator list are declared as constants that
>have type int and may appear anywhere such are permitted".

Note, however, that a good compiler can (and probably should) emit a
warning when enumeration constants for different enumerated types
are compared.  That is, given

	enum { apple, pear, orange } fruits;
	enum { lima, green, black } beans;

a good compiler would complain differently about the comparision:

	if (apple == orange)

than about the comparison:

	if (lima == pear)

Both of these are effectively the same as `if (0)', hence a good compiler
should warn about a constant in conditional context as well as mixing
enumeration types (in the second example) and code that is not reached
(assuming there are no labels following the comparisons).  All of these
are legal ANSI C expressions (despite the comparison of apples and oranges).

Incidentally, one apple is worth a lot of Lima beans. :-)
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab EE div (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list