Why does GCC warn only for situations 1 and 3 and not 2 in the code below ?
I'm compiling with -Wall and -g flags.
int main() {
unsigned int ui = 4;
int si = 6;
if (si == ui ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
if (si == 2U ) { // No Warning --- WHY ???
printf("xxxx");
}
if (si > 2U ) { // Warning comparison b/w signed and unsigned
printf("xxxx");
}
return 0;
}
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html:
-Wconversion section:
Do not warn for explicit casts like
abs ((int) x)
andui = (unsigned) -1
, or if the value is not changed by the conversion like inabs (2.0)
.
Since 2U
is literal, gcc know that:
si < 0
, then (unsigned) si >= 2^31
, therefore s1 != 2U
.si > 0
, then (unsigned) si
has the same value as si
, therefore (unsigned) si == 2U
if and only if si == 2
.In conclusion, comparing the signed si
with literal 2U
is the same as comparing si
with 2
, i.e., the result of si == 2U
would not be changed by converting si
to unsigned
.
If you compare with 2^32-1 (4294967295U), the largest in 32-bit unsigned int, which is not representable in int
, then si
could be equal to it even if si
itself is negative, this may not be what you wanted, so a warning is generated with -Wextra
option.
Possibly because there's no ambiguity in an equality comparison with constant in the range where signed and unsigned versions of the type overlap.
If I change it to
if (si == 2147483648U ) {
printf("xxxx");
}
I get a warning
(Actually, I had to add -Wextra before I got the warnings you reported)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With