The C99 standard states (6.4.4.1) that integer constants without a
suffix are converted to the first of the following types that can
represent their value:

  decimal constants: 1) int  2) long  3) long long
  hex/oct constants: 1) int  2) unsigned int  3) long  4) unsigned long
                     5) long long  6) unsigned long long

This works fine in the preprocessed code, but not within constant
preprocessor expressions, e.g.:

  #define XS 0x7fffffff

  #if XS > -1
  int xs = 1;
  #else
  int xs = 0;
  #endif

In this case, XS fits into an (int) and thus the comparison
is signed, resulting in xs == 1.

  #define XU 0x80000000

  #if XU > -1
  int xu = 1;
  #else
  int xu = 0;
  #endif

In this case, XU does not fit into an (int) and should rather
be an (unsigned), causing the -1 to be promoted and causing the
comparison to be unsigned, resulting in xu == 0.

However, it seems that the second case isn't handled correctly
by the gcc preprocessor, as it takes the xu == 1 path.

The attached code snippet shows that the gcc preprocessor doesn't
make a difference between dec/hex/oct constants, while the gcc
compiler does. The behaviour of the compiler looks correct to me.

Compile and run with:

  gcc -std=c99 -o const const.c
  ./const

I'd expect the output to be:

  1 - 0
  1 - 0
  1 - 0
  1 - 0
  1 - 1
  1 - 1

However, the output of my gcc-4.2 is

  $ gcc-4.2 -std=c99 -o const const.c && ./const
  1 - 1
  1 - 0
  1 - 1
  1 - 0
  1 - 1
  1 - 1

My gcc was compiled with:

  Using built-in specs.
  Target: i686-pc-linux-gnu
  Configured with: ../gcc-4.2-20060225/configure --program-suffix=-4.2
--prefix=/home/mhx/gcc/gcc-4.2-20060225 --enable-languages=c,c++ --disable-nls
  Thread model: posix
  gcc version 4.2.0 20060225 (experimental)

I've checked that the Intel C/C++ compiler (version 9.0)
produces the correct output:

  $ icc -std=c99 -o const const.c && ./const
  1 - 0
  1 - 0
  1 - 0
  1 - 0
  1 - 1
  1 - 1

Marcus


-- 
           Summary: hex and oct constants are converted to wrong type
           Product: gcc
           Version: 4.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: preprocessor
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: mhx-perl at gmx dot net
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27195

Reply via email to