[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

The C programming language 1.5.1 File Copying

Joseph Sanoyo

8/13/2011 12:34:00 AM

The part where he says:

"We can't use char since c must be big enough to hold EOF in addition
to any possible char. Therefore we use int.

Can someone explain this to me?
3 Answers

Seebs

8/13/2011 12:44:00 AM

0

On 2011-08-13, Joseph Santoyo <josephsantoyo@gmail.com> wrote:
> The part where he says:

> "We can't use char since c must be big enough to hold EOF in addition
> to any possible char. Therefore we use int.

> Can someone explain this to me?

What about it?

Okay, imagine that you want to have a function which gives you "the next
character from the file". So we might initially write:

char getchar();
and it returns the next character.

How should it indicate to you that there isn't a next character? Every
value this function could *possibly* return is a character that could occur
in a file.

So instead we write
int getchar();

Now, if there's a character, it returns a character, and if there's not
a character, it returns EOF, which is a value that isn't in the range
of unsigned char, so you can be sure it doesn't represent a character.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Joseph Sanoyo

8/13/2011 12:51:00 AM

0

On Aug 12, 8:44 pm, Seebs <usenet-nos...@seebs.net> wrote:
> On 2011-08-13, Joseph Santoyo <josephsant...@gmail.com> wrote:
>
> > The part where he says:
> > "We can't use char since c must be big enough to hold EOF in addition
> > to any possible char. Therefore we use int.
> > Can someone explain this to me?
>
> What about it?
>
> Okay, imagine that you want to have a function which gives you "the next
> character from the file".  So we might initially write:
>
>         char getchar();
> and it returns the next character.
>
> How should it indicate to you that there isn't a next character?  Every
> value this function could *possibly* return is a character that could occur
> in a file.
>
> So instead we write
>         int getchar();
>
> Now, if there's a character, it returns a character, and if there's not
> a character, it returns EOF, which is a value that isn't in the range
> of unsigned char, so you can be sure it doesn't represent a character.
>
> -s
> --
> Copyright 2011, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.nethttp://www.seebs.net/... lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.

Thanks, I understand it a lot better now.

Joe Pfeiffer

8/13/2011 5:04:00 AM

0

Joseph Santoyo <josephsantoyo@gmail.com> writes:

> The part where he says:
>
> "We can't use char since c must be big enough to hold EOF in addition
> to any possible char. Therefore we use int.
>
> Can someone explain this to me?

You pick a char value to mean EOF (let's assume an eight-bit char here,
so you've got 256 to choose from), and I'll give you a file containing
that char as a byte in the file, which your program will misinterpret as
being the end of the file.