Re: warning - comparing a signed value to an unsinged value
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 10/20/03
- Next message: Programmer Dude: "Re: why still use C?"
- Previous message: CBFalconer: "Re: [OT] Re: Pascal is better than C"
- In reply to: Kevin Goodsell: "warning - comparing a signed value to an unsinged value"
- Next in thread: Glen Herrmannsfeldt: "Re: warning - comparing a signed value to an unsinged value"
- Reply: Glen Herrmannsfeldt: "Re: warning - comparing a signed value to an unsinged value"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 20 Oct 2003 21:22:46 GMT
Kevin Goodsell wrote:
>
> What do you think is the best way to handle a compiler warning
> about comparing an unsigned value to a signed 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?
Spend a little time thinking. Assume we are talking about signed
and unsigned ints. Now, if the unsigned is larger than INT_MAX,
it is obviously larger than the int. If the int is negative, it
is obviously smaller than the unsigned. Having eliminated these
cases you can safely cast the int into unsigned, and then
compare. In fact, all you need to eliminate is the negative case.
-- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
- Next message: Programmer Dude: "Re: why still use C?"
- Previous message: CBFalconer: "Re: [OT] Re: Pascal is better than C"
- In reply to: Kevin Goodsell: "warning - comparing a signed value to an unsinged value"
- Next in thread: Glen Herrmannsfeldt: "Re: warning - comparing a signed value to an unsinged value"
- Reply: Glen Herrmannsfeldt: "Re: warning - comparing a signed value to an unsinged value"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|