www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/08/08/05:30:12

Xref: news2.mv.net comp.os.msdos.djgpp:6952
From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: warning problem
Date: Wed, 07 Aug 1996 21:36:04 -0700
Organization: Alcyone Systems
Lines: 52
Message-ID: <32096EB4.34EF4032@alcyone.com>
References: <4u9cju$p5p AT news DOT ysu DOT edu>
NNTP-Posting-Host: annex-p134.meer.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

randall williams wrote:

> while(x[b]!=10&&x[b]!=13&&x[b]!=EOF&&b<128)
> warning: comparison is always 1 due to limited range of data type
> 
> The above is the line causing the warning and I have an if() that
> causes a similar warning. Maybe I'm just missing something in my logic
> though I don't understand it. the variables are char x[], and int b.

The problem is that you've misreported the types of the variables in question
:-).  What's happening here is that your variable x is actually of type
unsigned char x[].

The comparison to the EOF macro is what's causing the warning.  According to
the ANSI C standard, section 7.9.1:

        EOF

    which expands to a negative integral constant expression that is 
    returned by several functions to indicate _end-of-file_, that is, no 
    more input from a stream. . . .

Since x[] is of type unsigned char[], any comparison to a negative integral
constant will itself be constant.  In this case your comparison is

    x[b] != EOF, 

where x[b] is of type unsigned char.  Since EOF is negative, this comparison
will always be true ("comparison is always 1 due to limited range of data
type").  An unsigned char can hold an ASCII character, but it can't hold the
value of EOF.  This means that that portion of your comparison will always
return 1 (true), and, as it's part of a repeated && expression, will add
nothing to the comparison; the expression behaves exactly as if you didn't
include the comparison at all.

I don't know exactly what context in which you're making this comparison
(since you only posted the while statement that was causing the warning), but
it looks to me as if you're reading a sequence of 128 characters into a buffer
and then iterating through the array to check for CR, LF, and EOF.  This is
the wrong context in which to check for EOF; EOF indicates an end-of-stream
condition and is not itself a character that will be read.  You should be
checking the function which is reading in the buffer (or line, if that's all
you're doing) for success or failure at that point, not checking the
characters of the array that is read after success or failure.

If you need more help, post a more complete sample of what you're trying to
do.

-- 
Erik Max Francis, &tSftDotIotE   http://www.alcyone.com/max/   max AT alcyone DOT com
San Jose, California   ICBM 37 20 07 N 121 53 38 W   R^4: the 4th R is respect
War's a game which were their subjects wise/Kings would not play at. -- Cowper

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019