6
votes

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?

4
So, what are you trying to accomplish? Under what conditions do you want this to return 1 as opposed to 0? - David Thornley
My intention was to get away this warning. - Günther Jena
Don't get away the warning, try to rid of your need to know if an unsigned is >= 0. It always is - that's what the compiler tells you. - xtofl
I want to have the possibility to change the definition of VALUE to another value than 0. If the VALUE is other than 0, it is no problem. - Günther Jena
Sounds like a good time to apply YAGNI to me. Wait until you actually need the feature. Or if you do actually need it, it probably shouldn't be a compile time constant but a config parameter or command line arg or what have you. - jpmc26

4 Answers

12
votes

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
}
7
votes

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 chars original value. Since this value is always >= 0, your if is always true.

3
votes

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.

1
votes

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.