rlkd@opusys.UUCP (R.L.K.Dattatri) (07/26/89)
IMPLEMENTATION OF 'ENUM' in A COMPILER Recently, while using two different compilers I ran into a problem with the types if 'enum' constants. One of them treated the 'enum' constants as 'integer' and the other one stored them as 'single bytes'. So if I compile parts of a program with these compilers, the results are bad. This could also happen if a library is made with one compiler and the source that uses the library is from a different compiler. Question: What is the standard (ANSI) type of an 'enum' constant? The ANSI-C standard just says 'Each enumerated type shall be compatible with an integer type; the choice of type is implementation defined'. From K&R 'The identifiers in an enumerator list are declared as constants of type int and may appear wherever constants are required.' Some compilers determine the type based on the number of constants in the 'enum' type. Some compilers provide an option to set the enum constants to 'int's. So what is the correct way of implementing 'enum'. |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| | R.L.K. Dattatri (408) 446-2110 (W) |~~~~~| | | Opus Systems (408) 243-5140 (H) |_____| | | 20863 Bldg 400 |\ | |/ |~~\ | | Stevens Creek, Cupertino | \ [___|\ | | | | California, 95014 \ | | | | E-mail: uunet!opusys!rlkd FAX: (408) 446-5009 \ |__/ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gwyn@smoke.BRL.MIL (Doug Gwyn) (07/26/89)
In article <396@opusys.UUCP> rlkd@opusys.UUCP (R.L.K.Dattatri) writes: >Recently, while using two different compilers I ran into a problem ... That is always a danger. Mixing C implementations on a single system is generally unsafe. There are many implementation-dependent aspects of C, and if two different compilers embody two different choices they may produce object modules that are incompatible. >Question: What is the standard (ANSI) type of an 'enum' constant? > The ANSI-C standard just says ... You just answered your own question. >Some compilers determine the type based on the number of constants >in the 'enum' type. Some compilers provide an option to set the >enum constants to 'int's. >So what is the correct way of implementing 'enum'. Both those alternatives are valid, as are some other possibilities. What is invalid is for "enum" to be treated as a type incompatible with integer operations. At least one release of PCC did that, and drove a lot of programmers up the wall.
dan@oresoft.uu.net (Daniel Elbaum) (07/27/89)
In article <396@opusys.UUCP> rlkd@opusys.UUCP (R.L.K.Dattatri) writes:
...
:Recently, while using two different compilers I ran into
:a problem with the types if 'enum' constants. One of them treated
:the 'enum' constants as 'integer' and the other one stored
:them as 'single bytes'...
:
:Question: What is the standard (ANSI) type of an 'enum' constant?
The relevant section of the dpANS of 12/88 is 3.5.2.2.
To paraphrase: every enumeration constant must be representable
by an int; the enumeration type may be any type compatible with
int. This means 'int', 'signed int', or 'unsigned int'.
It sounds like one of your compilers is noncompliant.
Enums were an early extension to C, so the first edition of K&R
doesn't define them.
--
The workaday world must remain transparent to those who maintain it if
they are to find inspired within them a vision of the history they create.
({uunet,tektronix,reed,sun!nosun,osu-cis,psu-cs}!oresoft!(dan)@oresoft.uu.net)
jbeard@quintus.UUCP (Jeff Beard) (07/27/89)
Having ported several programs using various C compilers, your problem is NOT enum per se ... but rather attempting to us multiple compilers for the same appliaction. I assume that the internal implementation yuou've discovered is not being used to access and data declared as 'enum'.... C portability ( if one is hanging his hat on the issue) is at the source level, not the binary *.o level. Thus attempting to bind several *.o from different compilers frought with environmental errors. One may clearly CC and test (assuming sufficient stubs) any code in any environment, but the real delivered executable had better be built from the same CC, using our friend 'make'.
walter@hpclwjm.HP.COM (Walter Murray) (07/28/89)
R.L.K.Dattatri writes: > Question: What is the standard (ANSI) type of an 'enum' constant? > [quotes from ANSI and K&R] From the rest of your posting, I think you may be confusing the enumerated type with the type of the enumeration constants. Consider the declaration: enum {red, green, blue} color; The identifier 'green' is an enumeration constant and is required to have type int. The identifier 'color' denotes an object which has enumerated type. That type has to be compatible with one of the eight integer types, and an ANSI-conforming implementation must document how that type is chosen. Walter Murray Not speaking for X3J11 ----------------------------------------------------------------------