Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > EOF

Reply
 
 
Richard Tobin
Guest
Posts: n/a
 
      09-19-2007
In article <. com>,
lak <> wrote:

>why EOF has an -1 value.what's the purpose to make it as -1. can any
>one tell answer for this?


It doesn't have to have the value -1. It has a negative value so that
it can be easily distinguished from real characters when returned from
getc() and the like.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
lak
Guest
Posts: n/a
 
      09-19-2007
why EOF has an -1 value.what's the purpose to make it as -1. can any
one tell answer for this?

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      09-19-2007
lak wrote:
> why EOF has an -1 value.what's the purpose to make it as -1. can any
> one tell answer for this?


RTFM is an obvious answer. Hint: The logic is essentially the same as
that which stipulates that getchar() (etc) return int not char...

I picked up my copy of K&R (first edition, somewhat battered and
found that at the time that was written there were two common EOF
conventions, one of which could be handled by getchar() returning a char
rather than an int... (Of course it had its own problems).
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-19-2007
lak wrote:
>
> why EOF has an -1 value.what's the purpose to make it as -1. can
> any one tell answer for this?


The only requirement for EOF is that it be negative.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-19-2007
lak wrote:
> why EOF has an -1 value.what's the purpose to make it as -1. can any
> one tell answer for this?


It doesn't. It could be any negative integral constant expression. The
reason is simple: to make EOF an encoding other than those used for
legitimate characters. That is why functions (or macros) like fgetc,
getc, and getchar return ints instead of chars. Did you check the FAQ
(or an elementary textbook) before posting? It's a good idea to do so;
many people, even the sweetest on occassion, get quite steamed at
elementary questions already answered in the FAQ.

 
Reply With Quote
 
bluey
Guest
Posts: n/a
 
      09-19-2007
On 19 Sep, 12:15, lak <lakindi...@gmail.com> wrote:
> why EOF has an -1 value.what's the purpose to make it as -1. can any
> one tell answer for this?


The actual answer is quite simple. you need to learn - as do many -
about the registers on the cpu itself. given that 0 in acsii is a
value (albiet a Null value) the only way a processor can see past this
is by checking the carry bit flag. by passing a -1 to the register it
can see a processor condition of carry (or not in this case0.


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-19-2007
bluey wrote:
> On 19 Sep, 12:15, lak <lakindi...@gmail.com> wrote:
>> why EOF has an -1 value.what's the purpose to make it as -1. can any
>> one tell answer for this?

>
> The actual answer is quite simple. you need to learn - as do many -
> about the registers on the cpu itself. given that 0 in acsii is a
> value (albiet a Null value) the only way a processor can see past this
> is by checking the carry bit flag. by passing a -1 to the register it
> can see a processor condition of carry (or not in this case0.


Either "bluey" is making a joke, which is in very poor taste since the
person asking the question may take him seriously, or he's an idiot. In
either case you should ignore his blathering.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-19-2007
bluey <> writes:
> On 19 Sep, 12:15, lak <lakindi...@gmail.com> wrote:
>> why EOF has an -1 value.what's the purpose to make it as -1. can any
>> one tell answer for this?

>
> The actual answer is quite simple. you need to learn - as do many -
> about the registers on the cpu itself. given that 0 in acsii is a
> value (albiet a Null value) the only way a processor can see past this
> is by checking the carry bit flag. by passing a -1 to the register it
> can see a processor condition of carry (or not in this case0.


Knowing about CPU registers is not necessary at all, or even
particularly helpful in this case.

fgetc() returns an int value which is either:
the next character read from the input stream (interpreted as an
unsigned char and converted to int;
or:
a distinct value, namely the value of the macro EOF, to indicate
end-of-file or an error condition.

The standard requires EOF to have a negative value. (It doesn't
require it to be -1; I've never heard of an implementation where it's
defined as anything other than -1, but I still wouldn't assume
anything beyond what the standard guarantees).

Since a valid unsigned char value can be any non-negative value in the
range 0 to UCHAR_MAX, it makes sense to use a negative value if you
need something distinct from all possible unsigned char values.

Note the cpomlete lack in this explanation of any mention of CPU
registers, condition codes, or carry bits. It's all about values, not
representations. And since most code checks whether the result is
equal (or unequal) to EOF, the fact that it's negative doesn't
typically even matter. The standard *could* have allowed EOF to be
defined as (UCHAR_MAX + 1), for example.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-19-2007
bluey <> writes:

> On 19 Sep, 12:15, lak <lakindi...@gmail.com> wrote:
>> why EOF has an -1 value.what's the purpose to make it as -1. can any
>> one tell answer for this?

>
> The actual answer is quite simple. you need to learn - as do many -
> about the registers on the cpu itself. given that 0 in acsii is a
> value (albiet a Null value) the only way a processor can see past this
> is by checking the carry bit flag. by passing a -1 to the register it
> can see a processor condition of carry (or not in this case0.


This answer is, at best, at the wrong level of abstraction for
the question. At worst, it is incorrect and misleading. I would
advise the OP to disregard it.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-20-2007
CBFalconer said:

> lak wrote:
>>
>> why EOF has an -1 value.what's the purpose to make it as -1. can
>> any one tell answer for this?

>
> The only requirement for EOF is that it be negative.


No, it must also have integral type. #define EOF -3.14159 would not be
conforming.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM
A question about EOF SL_McManus Perl 1 12-04-2003 01:50 AM
How to check for EOF (End of file) when using StreamReader to parse text file Sacha Korell ASP .Net 2 09-06-2003 02:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57