Are enums safe to use in portable code?
Stanley Friesen
sarima at tdatirv.UUCP
Tue Feb 5 08:48:22 AEST 1991
In article <ENAG.91Feb1003707 at hild.ifi.uio.no> enag at ifi.uio.no (Erik Naggum) writes:
>Assume these definitions:
> enum { frotz, klutz } foo; [1]
> enum { gunk, junk } bar; [2]
> ...
>The declaration in [1] declares a distinct (anonymous) type of which
>foo is an object. Likewise [2] declares another distinct (anonymous)
>type of which bare is an object.
So far so good. ANSI does indeed specify that each enumeration is a distinct
type.
> frotz and klutz [1] are names of
>constant values of the type of object of which foo is an instance.
>Likewise for gunk and junk [2].
However here you deviate from the ANSI standard.
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".
Thus frotz, klutz, gunk, and junk are all *int's*, and may be used anywhere
an int may be used. This includes *all* forms of comparison, thus:
(klutz == gunk)
is a perfectly valid ANSI C construct, and evaluates to true if and only if
the value of klutz equals the value of gunk. (This is also a constant
expression, that can be evaluated wholly by the compiler)
Any C compiler that fails to accept this is *not* ANSI C. (It may be
a valid pre-ANSI compiler though).
--
---------------
uunet!tdatirv!sarima (Stanley Friesen)
More information about the Comp.lang.c
mailing list