Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #67667

Re: Reading beyond the end of file

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Reading beyond the end of file
Date 2015-08-17 18:44 -0700
Organization None to speak of
Message-ID <lnk2st4bg0.fsf@kst-u.example.com> (permalink)
References <b62ba036-de31-4f37-aaad-3d1372d40d3f@googlegroups.com> <mqts2p$js0$1@dont-email.me>

Show all headers | View raw


Bartc <bc@freeuk.com> writes:
> On 18/08/2015 00:24, janus wrote:
>> I am not getting the expected result from the below code whenever I
>> change '\0' to EOF. I noticed that the below will read pass the end
>> of file. What is it that I am doing wrong? Could someone help me out?
>
> What's the input format? Is it a string terminated with \0, or with EOF?
>
> Note that EOF, normally set to -1, might not be properly representable 
> when the 'char' type is unsigned (it will be 255). It might work, but I 
> think 0 is a better terminator if it's going to be a char value.

EOF is the value returned by getchar() and related functions when they
encounter an end-of-file condition.

(Strictly speaking, it's a macro that expands to a constant expression
whose value is as I described.)

It never makes sense to store the value EOF in a char, signed char, or
unsigned char object.  It's intended as an in-band signal, distinct from
any valid input character.  getchar(), for example, returns an input
character interpreted as an unsigned char (and therefore >= 0) *or* the
value EOF (guaranteed to be negative).  If the input happens to have a
value of -1 when interpreted as a signed char, getchar() will return 255
(assuming CHAR_BIT==8).

[...]

C strings are by definition terminated by a `'\0'` character.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"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


Thread

Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-17 16:24 -0700
  Re: Reading beyond the end of file Bartc <bc@freeuk.com> - 2015-08-18 00:53 +0100
    Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-17 18:44 -0700
      Re: Reading beyond the end of file Tim Rentsch <txr@alumni.caltech.edu> - 2015-09-06 14:41 -0700
        Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-09-06 17:09 -0700
  Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-17 20:10 -0400
    Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 00:54 -0700
      Re: Reading beyond the end of file janus <emekamicro@gmail.com> - 2015-08-18 01:43 -0700
      Re: Reading beyond the end of file James Kuyper <jameskuyper@verizon.net> - 2015-08-18 10:18 -0400
      Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 09:18 -0700
        Re: Reading beyond the end of file Keith Thompson <kst-u@mib.org> - 2015-08-18 11:17 -0700
          Re: Reading beyond the end of file Barry Schwarz <schwarzb@dqel.com> - 2015-08-18 13:24 -0700

csiph-web