enums are treated as unsigned ints in ISO C standard


Problem : 
enum datatype is treated as unsigned int according to ISO C standard which leads to wrong results when compared with integer datatype in Intel® C++ Compiler for Linux*.

Following code illustrates this:--

////////////////////////////////////
#include <stdio.h>

typedef enum e
{
x = 10,
y ,
z
} e;

int main()
{
e i= (e)-1;
if (i>z)
printf("%dn",i);
else
printf("in else partn");
return 1;
}
The result with compilation in Linux* :--

icc test.c

Result:--

./a.out
-1    --> even if -1 is less than 12 .

The else part of the loop should have been executed

Environment : Linux* and Mac* OS

Resolution : 

Following are the workarounds:-- (to treat enums as per the ISO C++ standard)

1) Use -strict-ansi switch (/Za equivalent of Windows) in Linux* and Mac.

2) Use icpc compiler or rename test.c to test.cpp so that it is compiled as c++ file. then the result is:--

./a.out
"In else part."

Similarly, with gcc and g++.
Para obtener información más completa sobre las optimizaciones del compilador, consulte nuestro Aviso de optimización.