I have a simple code like this:
typedef enum { N1 = 1, N2 = 2 } EnumType; int main(void) { EnumType A = N1; if( A >= 0 ) return 1; return 0; }
Comparison with zero gives me warning "#186-D: pointless comparison of unsigned integer with zero".
I then go to project options and check "Enum container always int".
If this code was in *.cpp file - everything is fine, warning goes away (as it should). But if it's in *.c file - warning is still there!
Compiler key --cpp has the same effect.
This behaviour is very annoying, actually, because I have an entire library filled with such comparisons (and warnings).
Can anyone please tell me, how to remove this very annoying warning and is it a bug or not?
I'm using ARM Keil 4.72.
P.S. I know that I can just disable this warning with a compiler suppressing option (and that's evil by itself) or with pragma but that is not very good solution.
I'm curious. In what way would your variable get a negative value, when your enum doesn't contain any negative values. Are you playing with typecasts, or are you just trying to detect invalid variable states?
Per Westermark The problem is that this sort of code is inside the library. It contains lots of safety checks for input variables. I guess, since in C int can be implicitly converted to enum (and vice versa) that does make sense. I'm not the author of the library, so I can only guess.
I also thought that any kind of enum should actually be signed int, so this warning will not even exist with some other compiler (but I can be wrong).