Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Compiler warning about comparison always false |
| Date | 2012-03-07 14:36 -0800 |
| Organization | None to speak of |
| Message-ID | <ln399k598d.fsf@nuthaus.mib.org> (permalink) |
| References | <c0930b34-a2df-4588-9fb2-1ae3d33785e4@or10g2000pbc.googlegroups.com> <jj8gef$4mh$1@blue-new.rahul.net> |
falk@rahul.net (Edward A. Falk) writes:
> In article <c0930b34-a2df-4588-9fb2-1ae3d33785e4@or10g2000pbc.googlegroups.com>,
> Nachy <mazwolfe@gmail.com> wrote:
>
>>I separated the comparisons, and gcc seems to be complaining about the
>>last one (ch == 0xFF).
>
> Your compiler uses signed characters, which can't have a value greater
> than 0x7F. 0xFF is an integer, so the comparison will first be promoting
> ch to an integer.
More precisely, 0xFF is an int. The word "integer" covers a variety of
types, including both int and char.
> The compiler knows that ch will never have the value
> 0xFF and so the comparison will always be false, which is probably not
> what you had in mind, so the compiler is doing you a big favor and
> warning you that this code will not do what you want.
Maybe. But if the code is intended to be portable to systems where
plain char is either signed or unsigned, the fact that the comparison is
always true on some systems doesn't mean it doesn't make sense on
others.
> Fix:
>
> ch == (char)0xFF
I'd say that's more of a workaround. The result of converting an
out-of-range value to a signed type is implementation-defined (it can
even raise a signal). If plain char is signed and CHAR_BIT==8, then
(char)0xFF is *probably* -1, but that's not guaranteed.
A better solution, as other responses have said, is probably to change
the type of ch, most likely to int.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Compiler warning about comparison always false Nachy <mazwolfe@gmail.com> - 2012-03-07 09:25 -0800
Re: Compiler warning about comparison always false bert <bert.hutchings@btinternet.com> - 2012-03-07 09:32 -0800
Re: Compiler warning about comparison always false Nachy <mazwolfe@gmail.com> - 2012-03-07 09:42 -0800
Re: Compiler warning about comparison always false Philip Lantz <prl@canterey.us> - 2012-03-07 10:17 -0800
Re: Compiler warning about comparison always false Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 15:30 -0700
Re: Compiler warning about comparison always false Philip Lantz <prl@canterey.us> - 2012-03-14 00:55 -0700
Re: Compiler warning about comparison always false James Kuyper <jameskuyper@verizon.net> - 2012-03-07 14:06 -0500
Re: Compiler warning about comparison always false Fred <fred.l.kleinschmidt@gmail.com> - 2012-03-07 11:11 -0800
Re: Compiler warning about comparison always false "io_x" <a@b.c.invalid> - 2012-03-07 18:51 +0100
Re: Compiler warning about comparison always false falk@rahul.net (Edward A. Falk) - 2012-03-07 20:29 +0000
Re: Compiler warning about comparison always false Keith Thompson <kst-u@mib.org> - 2012-03-07 14:36 -0800
Re: Compiler warning about comparison always false Markus Wichmann <nullplan@gmx.net> - 2012-03-07 21:26 +0100
Re: Compiler warning about comparison always false Kaz Kylheku <kaz@kylheku.com> - 2012-03-14 17:49 +0000
csiph-web