Printing the value of EOF

bigTree picture bigTree · May 24, 2014 · Viewed 24k times · Source

In Kernighan and Ritchie (the C programming language):

'Write a program to print the value of EOF'

I wrote:

#include <stdio.h>

main(){

    int c;
    c = getchar();
    if ((c = getchar()) ==  EOF)
        putchar(c);
}

but it doesn't output anything Why?

Answer

Sorcrer picture Sorcrer · May 24, 2014

putchar function prints a character.

But EOF is not a character and is used to indicate the End of a file. So the getchar returns a value which is distinguishable from the character sets so as to indicate there is no more input.

So printing EOF using putchar() wont print any values

printing it as integer

printf("%d",EOF);

gives result -1