Mail Archives: djgpp/1998/08/25/08:38:01
On Mon, 24 Aug 1998, Mariano Alvarez Fernández wrote:
> #include <conio.h>
> typedef enum { V1, V2, V3 } Tipo;
> int main()
> {
> Tipo i;
> i = -1;
> if( i < 0 )
> puts( "Negative number" );
> else
> puts( "Positive number" );
> }
[snip]
> 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).
No, this is a bug in your program. K&R book notwithstanding, you
cannot assume anything about how enums are implemented, unless you
specify this to the compiler (see below). The ANSI C standard
explicitly says that the representation of enum is
implementation-defined.
Moreover, you are NOT supposed to assign any value to the enum
variable except those in the enum definition. In your case, you can
only assign V1, V2, or V3 to i; you cannot assign -1.
Your example should be rewritten like this:
typedef enum { INVALID = -1, V1, V2, V3 } Tipo;
int main (void)
{
Tipo i;
i = INVALID;
if (i == INVALID)
puts ("Invalid value");
else
puts ("Valid value");
return 0;
}
Note that you cannot compare enum variables except with one of the
values listed in the definition, or against another variable of the
same enum. You cannot compare to arbitrary integer constants.
- Raw text -