cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/25/08:38:01

Date: Tue, 25 Aug 1998 15:24:15 +0300 (IDT)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Mariano Alvarez Fernández <malfer AT teleline DOT es>
cc: djgpp AT delorie DOT com
Subject: Re: Possible bug in GCC
In-Reply-To: <35E1A919.45F5@teleline.es>
Message-ID: <Pine.SUN.3.91.980825152355.7520h-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019