gcc compiler bug: unsigned enum types

Alan Morgan arm@thebe.camcon.co.uk
Mon Sep 11 03:44:00 GMT 2000


This is a bug report I am passing on to you from a colleague of mine, Roger
Sewell (rfs@camcon.co.uk). Below is Roger's email showing the commands he used
to demonstrate the bug. I have attached the source code for his program
bugdemo.c to the end of this email.

The bug is that gcc makes "an enum type be of type unsigned int (or maybe
unsigned char) rather than signed int as it should be according to K&R 2nd
edition section A8.4 pages 214-215."

This bug first showed itself while we were running gcc 2.8.1, but I have now
upgraded the system to 2.95.2 and the behaviour is unchanged. If one compiles
the same program with -fshort-enums, then the bug is not present. i.e. the
type is then signed.

We are running gcc 2.95.2 on a Sun Ultra system (an E450), running Solaris
2.6. For the build, the only argument given explicitly to configure was
"--prefix=/usr/ccl/src/gcc_2.95"

Best Regards,

-- 
Dr. Alan Morgan            |                                 
Cambridge Consultants Ltd. |  Tel:  +44 (0)1223 420024 (switchboard)  
Science Park               |        +44 (0)1223 392682 (direct)
Milton Road                |  
Cambridge                  |  Fax:  +44 (0)1223 423373   
CB4 0DW                    |  Email: arm@camcon.co.uk   
England                    |                                 


------- start of forwarded message (RFC 934 encapsulation) -------
Return-Path: <rfs@athene.camcon.co.uk>
Received: by camcon.co.uk (SMI-8.6/SMI-SVR4)
	id RAA21074; Mon, 3 Jul 2000 17:50:30 +0100
Message-Id: <200007031650.RAA21074@camcon.co.uk>
Content-Length: 618
From: Roger Sewell <rfs@athene.camcon.co.uk>
To: arm
Subject: Please could you have a look at....
Date: Mon, 3 Jul 2000 17:50:30 +0100


Alan,

I think the following demonstrates a bug in gcc, and would be most
grateful for a second opinion and if you're willing a report to
whoever the appropriate person is.

Program is in ~rfs/dbn/bugdemo.c

Compile it with 

gcc -o bugdemo bugdemo.c or 

or with

cc -o bugdemo bugdemo.c

then run it, follow the instructions, and observe the difference in
output depending what it was compiled with.

Then check that you agree with my comments at the top of the program
as to the reason.

If you agree with my interpretation, would you feel inclined to report
it to whoever the relevant person is ?

Thanks,
Roger.

------- end -------

------8<----Here follows bugdemo.c------8<------- 
#include <stdio.h>

/* The following program demonstrates gcc making an enum type
 * be of type unsigned int (or maybe unsigned char) rather
 * than signed int as it should be according to K&R 2nd edition
 * section A8.4 pages 214-215.
 *
 * Having compiled it, answer as suggested to the prompts (i.e. not just
 * with carriage return!).
 */

int
main(int argc, char **argv)
{
  typedef enum {FALSE, TRUE} bool;

  int input_integer;

  bool z;
  int x;
  unsigned int y;

  int sx, sy, sz, six, siy, siz;
  double a, sxa, sya, sza, sixa, siya, siza;

  printf("Answer as suggested to the following prompts:\n");

  printf("Input value of a (3.1) ? ");
  scanf("%lg", &a);

  printf("Input value of x (0) ? ");
  scanf("%d", &input_integer);

  x = input_integer;
  y = input_integer;
  z = input_integer;

  sx = 2 * x - 1;
  six = 2 * (int) x - 1;

  sy = 2 * y - 1;
  siy = 2 * (int) y - 1;

  sz = 2 * z - 1;
  siz = 2 * (int) z - 1;

  sxa = (2 * x - 1) * a;
  sixa = (2 * (int) x - 1) * a;

  sya = (2 * y - 1) * a;
  siya = (2 * (int) y - 1) * a;

  sza = (2 * z - 1) * a;
  siza = (2 * (int) z - 1) * a;

  printf("x = %d, y = %u, z = %d\n",
         x, y, z);

  printf("a = %g\n",
         a);

  printf("The values of all the following except sya should be minus the value of a:\n");
  printf("sxa = %g, sya = %g, sza = %g\n",
         sxa, sya, sza);

  printf("sixa = %g, siya = %g, siza = %g\n",
         sixa, siya, siza);

  printf("sizeof bool = %d\n", sizeof(z));
  

  return(0);
}



More information about the Gcc-bugs mailing list