Re: Comparing singed to unsinged warning



On Aug 22, 4:09 pm, Nevil Lesdog <n...@xxxxxxxxx> wrote:
What do you think is the best way to handle a compiler warning about
comparing an unsinged value to a singed value? Cast to silence it?
Disable that warning altogether? Or just live with it?

On one hand, the warning *could* be useful. Most of the time I get it in
cases where I know the comparison is safe, but it's not hard to imagine
that this won't always be the case. This makes disabling it undesirable.
Casting is a workable solution, but I worry that changes in the code
later could introduce errors that go undetected due to the cast. And I
think we all hate not having a "clean" compile (if only because having a
bunch of warnings that you expected makes it more difficult to spot the
ones you didn't expect).

What is your opinion?

You could compare them using a dedicated function like in this
program:

#include <stdio.h>
#include <limits.h>

int CmpSU (int s, unsigned int u)
// compares signed int s and unsigned int u;
// returns: -1 if s < u
// 0 if s == u
// 1 if s > u
{
if (s >= 0)
{
if ((unsigned int)s < u) return -1;
else if ((unsigned int)s > u) return 1;
return 0;
}
return -1;
}

struct
{
int s;
unsigned u;
} aTestValues[] =
{
{INT_MIN, 0},
{INT_MIN, 1},
{INT_MIN, INT_MAX},
{INT_MIN, UINT_MAX},
{-1, 0},
{-1, 1},
{-1, INT_MAX},
{-1, UINT_MAX},
{0, 0},
{0, 1},
{0, INT_MAX},
{0, UINT_MAX},
{1, 0},
{1, 1},
{1, INT_MAX},
{1, UINT_MAX},
{INT_MAX, 0},
{INT_MAX, 1},
{INT_MAX, INT_MAX},
{INT_MAX, UINT_MAX},
};

int main (void)
{
int i;
for (i = 0;
i < sizeof(aTestValues)/sizeof(aTestValues[0]);
i++)
{
int s = aTestValues[i].s;
unsigned int u = aTestValues[i].u;
printf ("%d %c %u\n",
s,
"<=>"[1+CmpSU(s,u)],
u);
}
return 0;
}


Alex

.



Relevant Pages

  • Re: Comparing singed to unsinged warning
    ... comparing an unsinged value to a singed value? ... Look at the code and make darned sure you understand why the warning ... cast, which will at least indicate that you did it on purpose. ... This makes disabling it undesirable. ...
    (comp.lang.c)
  • Re: size_t and int comparison
    ... > you are comparing it to an int, a signed type. ... > Two solutions to remove the warning: ...
    (comp.lang.cpp)
  • warning - comparing a signed value to an unsinged value
    ... What do you think is the best way to handle a compiler warning about ... comparing an unsigned value to a signed value? ... This makes disabling it undesirable. ... later could introduce errors that go undetected due to the cast. ...
    (comp.lang.c)
  • Comparing singed to unsinged warning
    ... What do you think is the best way to handle a compiler warning about ... comparing an unsinged value to a singed value? ... This makes disabling it undesirable. ... later could introduce errors that go undetected due to the cast. ...
    (comp.lang.c)
  • Re: when to use uint? dis/advantages?
    ... > Just wondering what are the dis/advantages of using uint vs int. ... There is no such type as uint. ... > and calling foobarwill generate a warning. ... If you're comparing against a int, then you ought to use a ...
    (comp.lang.cpp)

Loading