Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | bert <bert.hutchings@btinternet.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Compiler warning about comparison always false |
| Date | 2012-03-07 09:32 -0800 |
| Organization | http://groups.google.com |
| Message-ID | <11715727.4312.1331141528977.JavaMail.geo-discussion-forums@vbze11> (permalink) |
| References | <c0930b34-a2df-4588-9fb2-1ae3d33785e4@or10g2000pbc.googlegroups.com> |
On Wednesday, March 7, 2012 5:25:57 PM UTC, Nachy wrote:
> Hi, all.
>
> I got an interesting warning message from my compiler, and before I
> complain about a bug I want to make sure what's going on.
>
> I have a method disp_char, meant to append a character to a string,
> or a period if the character is a control character. The string better
> be the right size (it is, that's not the issue).
>
> int disp_char(int c, char *s)
> {
> char ch = c & 0xFF; // make sure it's a char
> if (ch < 0x20 || ch == 0x7F || ch == 0xFF) // special character?
> *s = '.';
> else
> *s = ch;
> *++s = '\0';
>
> return 1;
> }
>
> On one system I compiled with, gcc gave me a warning:
> $ gcc -o ../dump dump.c
> dump.c: In function `disp_char':
> dump.c:140: warning: comparison is always false due to limited range
> of data type
>
> I separated the comparisons, and gcc seems to be complaining about the
> last one (ch == 0xFF).
>
> Am I doing something wrong, or is gcc being paranoid?
>
> BTW, on another system, gcc did not complain at all, even with -Wall.
> The program seems to work fine, although I can't seem to generate a
> test case with an actual file.
You'd expect a warning on a system where the range
of type 'char' was from -128 to +127, and not a
warning on a system where it's from 0 to +255.
Maybe you should be doing the comparisons with
the int result of (c & 0xFF), instead of the
char result, because the constants that you're
comparing with aren't of type 'char'.
--
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