Issue
I was reading the C programming language book and in the given below code I am not quite getting how will we be getting an EOF here? And also I don't exactly get what is an EOF? is it a character, integer or a condition? If it is a condition then please explain. Thank you.
#include <stdio.h>
int main(int argc, char *argv[]){
int c;
while((c = getchar()) != EOF){
printf("%d", c != EOF);
putchar(c);
}
printf("\n%d\n", c != EOF);
}
Solution
The getchar
function can return any possible character that can be represented on your system, plus one more, additional, special, "out-of-band" return value indicating that there are no more characters to be returned. This value is referred to by the symbolic macro identifier EOF
. Somewhere behind <stdio.h>
there is typically a definition like
#define EOF (-1)
The value isn't necessarily -1, but that will serve our purposes. (We don't have to know or care what the actual value is, because we always refer to EOF
, insulating us from the actual value.)
Whatever value EOF
has, it's guaranteed not to be the same as any actual character. So if getchar
returns a value other than EOF
, it's an actual character that has been read. But if it returns EOF
, that's the indication that the input stream has reached end-of-file, that there are no more characters to be read.
So EOF
is most definitely not a character. It is an integer, with a value different than any char
. I wouldn't call it a "condition", although it you test for it, using code like
if(c == EOF)
or
if(c != EOF)
I would say that those expressions c == EOF
and c != EOF
are conditions.
It's especially confusing because typically there's a special character you can type on the keyboard to generate an end-of-file indication. On Windows it's control-Z; on Unix and other systems it's control-D. But this does not mean that EOF
is control-Z on a Windows system or that it's control-D on a Unix system. Those characters are handled by the terminal driver and translated into an indication that, after several additional levels of handling and interpretation, causes getchar
to return EOF
. But one way to prove that EOF
is not control-Z or control-D is to observe that when you're using getchar
or getc
to read from a file, you get an EOF
indication at end-of-file even though there's no one typing control characters at a keyboard in that case.
Finally, this discussion reminds us of the importance of using int
variables to carry the values returned by getchar
. The code you cited correctly declared
int c;
and then used the variable c
to hold the values returned by getchar
. Why use an int
here? Since getchar
returns characters, and since c
is a character, wouldn't it make more sense to declare
char c;
That seems like it makes sense, but it's actually quite wrong. Type char
can hold any character the system can represent. But remember, getchar
can return any character value, plus the EOF
value. If you assign getchar
's return value to a variable of type char
, you end up "scraping off" the EOF indication, by inadvertently mapping it to an actual character value. What happens next is either that you map an actual character (usually ÿ) to EOF
and mistakenly stop reading when you read one, or that you map EOF
to an actual character and so never detect it and so go into an infinite loop instead of stopping at end-of-file.
Answered By - Steve Summit Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.