Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Reading beyond the end of file Date: Sun, 06 Sep 2015 17:09:30 -0700 Organization: None to speak of Lines: 39 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="22158"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rsbxtJH5tABwhRtYuO+fO" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:+6tgFmj0udSl9UZyIwAAyNB8F78= sha1:KfqE9DYtXU9Fy1Tmkgn7+4u8xgU= Xref: csiph.com comp.lang.c:69700 Tim Rentsch writes: > Keith Thompson writes: >> Bartc 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. [snip] > > Presumably you meant to say out-of-band rather than in-band. No, I meant to say in-band. It's possible I misused the term, but I don't think so. Calling getchar() repeatedly gives you a sequence of int values. Most of those values are going to represent character values in the range 0..UCHAR_MAX. The value EOF in the same sequence does not represent a character value. -- Keith Thompson (The_Other_Keith) kst-u@mib.org 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"