Message-ID: <35E26308.E4431CB2@pacificnet.net> Date: Tue, 25 Aug 1998 00:08:57 -0700 From: Ralph Gesler MIME-Version: 1.0 To: malfer AT teleline DOT es CC: djgpp AT delorie DOT com Subject: Re: Possible bug in GCC References: <35E1A919 DOT 45F5 AT teleline DOT es> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Precedence: bulk Mariano Alvarez Fernández wrote: > Finally I had catched a strange bug porting a program to DJGPP. It > relies with enum vars. The next example program isolates the problem: > > #include > typedef enum { V1, V2, V3 } Tipo; > int main() > { > Tipo i; > i = -1; > if( i < 0 ) > puts( "Negative number" ); > else > puts( "Positive number" ); > } > > This program writes with: > > DJGPP 2.01 (GCC 2.8.1) : Positive number. > Debian-Linux 1.3 (GCC 2.7.2.1): Positive number. > BC++ 3.0 : Negative number. > HP-UX C (10.10) : Negative number. > HP C/1000 (6.2) : Negative number. > > I think it's a GCC bug because the K&R book (second ed.) say enum types > default to int (not unsigned as DJGPP does). I know I must report that > to the GCC maintainer, but I want to know yours opinions first. > > Regards, M.Alvarez I don't know how the C standard defines the underlying type of enumeration, but in C++, which I suspect is compatable with C, it is implementation dependent. From the working papers of the C++ standard. The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required. If no enumerator-defini- tions with = appear, then the values of the corresponding constants begin at zero and increase by one as the enumerator-list is read from left to right. An enumerator-definition with = gives the associated enumerator the value indicated by the constant-expression; subsequent enumerators without initializers continue the progression from the assigned value. The constant-expression shall be of integral or enu- meration type. 2 [Example: enum { a, b, c=0 }; enum { d, e, f=e+2 }; defines a, c, and d to be zero, b and e to be 1, and f to be 3. ] 3 The point of declaration for an enumerator is immediately after its enumerator-definition. [Example: const int x = 12; { enum { x = x }; } Here, the enumerator x is initialized with the value of the constant x, namely 12. ] 4 Each enumeration defines a type that is different from all other types. The type of an enumerator is its enumeration. 5 The underlying type of an enumeration is an integral type that can represent all the enumerator values defined in the enumeration. It is implementation-defined which integral type is used as the underlying type for an enumeration except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0. The value of sizeof() applied to an enumeration type, an object of enumer- ation type, or an enumerator, is the value of sizeof() applied to the underlying type This may explain the different results for the various compilers. AFAIK gcc is ANSI C complient. Ralph Gesler.