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?
EOF
is not a character, thereforeputchar(EOF)
can do anything. What you want isprintf("%d", EOF)
. – Exclosureputchar(EOF)
is defined to meanputchar( (unsigned char)(EOF) )
(C11 7.21.7.3) , so it can't "do anything" as such, it must output some character. Although perhaps that is a non-printable character – Bagel