Although the problem is small, is has a lot going on
typedef enum {bar,baz,last} en;
en foo = baz;
for(int i =bar; i<=last ; i++)
if(i==foo){
As well answered by @lundin:
* The en
constants are int
* Objects of type en
are char
, a signed integer type, or an unsigned integer type.
Code has 2 compare function with int
and en
.
i<=last
i==foo
Yet As I understand, only the 2nd one warns about "comparison of integer expressions of different signedness: ‘int’ and ‘en’".
In the first case, i<=last
: last
is a en
constant, so that is simple int <= int
--> no warning.
But surprisingly had code been unsigned i
, i<=last
does not warn even it is now a "integer expressions of different signedness".
Changing the last is still a "integer expressions of different signedness" yet no warning.
// i==foo
(i&1)==foo
Conclusion "integer expressions of different signedness" warning happens when 2 conditions are met:
- Types are of different sign (no rocket science on this one).
- The compiler concludes the range of possible values of the signed side and the range of the values of the unsigned are not both in the positive signed range - or something like that.
Although our human analysis of for(int i =bar; i<=last ; i++) if(i==foo){ printf("%i\n",i); }
sees no compare problem here, the compiler missed it.