Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   EOF (http://www.velocityreviews.com/forums/t538425-eof.html)

Richard Tobin 09-19-2007 11:09 AM

Re: EOF
 
In article <1190200502.002703.71650@i38g2000prf.googlegroups. com>,
lak <lakindia89@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?


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.

lak 09-19-2007 11:15 AM

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


Mark Bluemel 09-19-2007 11:32 AM

Re: EOF
 
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).

CBFalconer 09-19-2007 01:43 PM

Re: EOF
 
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


Martin Ambuhl 09-19-2007 06:20 PM

Re: EOF
 
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.


bluey 09-19-2007 10:56 PM

Re: EOF
 
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.



Martin Ambuhl 09-19-2007 11:39 PM

Re: EOF
 
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.

Keith Thompson 09-19-2007 11:42 PM

Re: EOF
 
bluey <luappy13@hotmail.com> 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-u@mib.org <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"

Ben Pfaff 09-19-2007 11:43 PM

Re: EOF
 
bluey <luappy13@hotmail.com> 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 1:putchar(a[i&15]);break;}}}

Richard Heathfield 09-20-2007 01:31 AM

Re: EOF
 
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


All times are GMT. The time now is 05:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.