This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
how to compile enum as signed independent of enum value range?
- From: "Michael Brand" <michael dot brand at nexustelecom dot com>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Tue, 14 Aug 2007 12:42:28 +0200
- Subject: how to compile enum as signed independent of enum value range?
So far I found out how to tell gcc to change compiling the enum B from
unsigned to signed as you can see below:
$ cat enum.c
/* 1 */ #include <limits.h>
/* 2 */ enum A {AA = -1};
/* 3 */ enum B {BB = UINT_MAX};
/* 4 */ enum C {CC0 = -1, CC1 = UINT_MAX - 1};
/* 5 */ enum D {DD = 0};
/* 6 */ int main(void) {
/* 7 */ enum A a;
/* 8 */ enum B b;
/* 9 */ enum C c;
/* 10 */ enum D d;
/* 11 */ a = (a >= 0);
/* 12 */ b = (b >= 0);
/* 13 */ c = (c >= 0);
/* 14 */ d = (d >= 0);
/* 15 */ return a * 1000 + b * 100 + c * 10 + d;
/* 16 */ }
$
$ gcc --version
gcc (GCC) 3.4.6
...
$
$ gcc -Wall -Wextra enum.c
enum.c: In function `main':
enum.c:12: warning: comparison of unsigned expression >= 0 is always
true
enum.c:14: warning: comparison of unsigned expression >= 0 is always
true
$
$ gcc -Wall -Wextra -ansi enum.c
enum.c: In function `main':
enum.c:12: warning: comparison of unsigned expression >= 0 is always
true
enum.c:14: warning: comparison of unsigned expression >= 0 is always
true
$
$ gcc -Wall -Wextra -ansi -pedantic enum.c
enum.c:3: warning: ISO C restricts enumerator values to range of `int'
enum.c:4: warning: ISO C restricts enumerator values to range of `int'
enum.c: In function `main':
enum.c:14: warning: comparison of unsigned expression >= 0 is always
true
$
$ gcc -Wall -Wextra -pedantic enum.c
enum.c:3: warning: ISO C restricts enumerator values to range of `int'
enum.c:4: warning: ISO C restricts enumerator values to range of `int'
enum.c: In function `main':
enum.c:14: warning: comparison of unsigned expression >= 0 is always
true
$
It is not my intention to change the behaviour of these options.
My questions:
How to tell gcc to compile enum D as signed too but without changing the
source code? I don't want to have a negative enum value in enum D like
AA in enum A or to add a signed cast to the evaluation of d.
I expected that it would be possible to do this for enums with a gcc
option -fsigned-enum like it is possible to do this for bitfieds or char
with -fsigned-bitfield or -fsigned-char. Shouldn't -fsigned-enum and
-funsigned-enum be added to gcc to facilitate porting of code to compile
the same way with gcc too?