Are enums safe to use in portable code?

Dave P. Schaumann dave at cs.arizona.edu
Fri Feb 1 09:06:23 AEST 1991


bliss at sp64.csrd.uiuc.edu (Brian Bliss) writes:
BB> The alliant fx C compiler (for an fx1 or fx8, not fx2800), supports
BB> them, but gives errors when you try to compare two enum constants.

steve at taumet.com (Stephen Clamage) writes:
SC>Enums are well-defined and safe in standard (ANSI/ISO) C.  However,
SC>[there are pre-ANSI compilers out there that get them wrong].
SC>So if you need to port code among lots of pre-standard
SC>or non-standard compilers, it is probably best not to use enums.

(sarcasm 'on)
Of course, if you want to port to a machine that doesn't have a C compiler
at all, you'd better not even use C.  Maybe you'll want to port to a machine
that doesn't have a compiler that allows floating point numbers.  Better not
use those, either.

Let us all now observe a moment of silence for the god Portability.
(sarcasm 'off)

(soapbox 'on)
Portability is a slippery issue.  People like to treat it like absolute
portability is a reasonable and possible goal.  But it is not possible to
write a program that is absolutely portable to every platform on God's Green
Earth.  (By absolutely portable, I mean compilable/interpretable with *no*
modifications whatsoever).  Absolute portability is a grail no-one can reach.

Realistic portability is balancing a trade-off.  The wider the range of
computers you wish to port to, the fewer language choices you have, and the
fewer language features you can count on to be there and work right.

Even if you restrict yourself to platforms that have some kind of C compiler,
you are still faced with a wildly varying field of language features and
qualities of implementation.  To have even realistic portability, you have
to expect at least a little editing on the target machine (in the general
case).
(soapbox 'off)

If you're porting a program that uses enums to a compiler that doesn't have
enums, or has broken enums, it is a fairly simple proceedure (in fact, it
can be automated) to use #defines as a work-around.

Dave Schaumann		|  And then -- what then?  Then, future...
dave at cs.arizona.edu	|  		-Weather Report



More information about the Comp.lang.c mailing list