The C programming language 1.5.1 File Copying

J

Joseph Santoyo

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?
 
S

Seebs

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
 
J

Joseph Santoyo

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

Thanks, I understand it a lot better now.
 
J

Joe Pfeiffer

Joseph Santoyo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top