Reputation: 3776
I have a small sample function:
#define VALUE 0
int test(unsigned char x) {
if (x>=VALUE)
return 0;
else
return 1;
}
My compiler warns me that the comparison (x>=VALUE) is true in all cases, which is right, because x is an unsigned character and VALUE is defined with the value 0. So I changed my code to:
if ( ((signed int) x ) >= ((signed int) VALUE ))
But the warning comes again. I tested it with three GCC versions (all versions > 4.0, sometimes you have to enable -Wextra).
In the changed case, I have this explicit cast and it should be an signed int comparison. Why is it claiming, that the comparison is always true?
Upvotes: 6
Views: 4045
Reputation: 127447
Even with the cast, the comparison is still true in all cases of defined behavior. The compiler still determines that (signed int)0
has the value 0, and still determines that (signed int)x)
is non-negative if your program has defined behavior (casting from unsigned to signed is undefined if the value is out of range for the signed type).
So the compiler continues warning because it continues to eliminate the else case altogether.
Edit: To silence the warning, write your code as
#define VALUE 0
int test(unsigned char x) {
#if VALUE==0
return 1;
#else
return x>=VALUE;
#endif
}
Upvotes: 12
Reputation: 7378
All the values of an unsigned char
can fir perfectly in your int
, so even with the cast you will never get a negative value. The cast you need is to signed char
- however, in that case you should declare x
as signed
in the function signature. There is no point lying to the clients that you need an unsigned value while in fact you need a signed one.
Upvotes: 3
Reputation: 4689
x
is an unsigned char
, meaning it is between 0 and 256. Since an int
is bigger than a char
, casting unsigned char
to signed int
still retains the char
s original value. Since this value is always >= 0, your if
is always true.
Upvotes: 7
Reputation: 69380
The #define
of VALUE
to 0
means that your function is reduced to this:
int test(unsigned char x) {
if (x>=0)
return 0;
else
return 1;
}
Since x
is always passed in as an unsigned char
, then it will always have a value between 0
and 255
inclusive, regardless of whether you cast x
or 0
to a signed int
in the if
statement. The compiler therefore warns you that x
will always be greater than or equal to 0
, and that the else
clause can never be reached.
Upvotes: 1