3

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);
}

3 Answers3

5

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.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • Thank you great explanation, that did clear much of my confusions. Although how will I get EOF in the above code? – gunjanpatait Jul 23 '18 at 01:47
  • 2
    @gunjanpatait When you run the program, try typing "A B C " on a Windows system, or "A B C " on a Unix, Linux, or Mac system. – Steve Summit Jul 23 '18 at 01:49
2

EOF means end of file. When you read a input stream (file, keyboard, etc) EOF is returned when the stream is over . EOF is commonly represented by the int -1

Eduardo Soares
  • 992
  • 4
  • 14
-1

EOF is Condition. Now Let's Take An Example. You have 1 .txt file in which you have written 50 lines but you doesn't know that how many characters are there in that .txt file. Now you want to print all character from that .txt file to your c language program but still you doesn't know how many characters are there. for that you have to use EOF (Means End OF File) which is being condition of your for loop or while loop. and this loop will be continued to the last character of your .txt file.