cputchar

Printing the value of EOF


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?


Solution

  • 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