Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the value of EOF

Tags:

c

putchar

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?

like image 794
bigTree Avatar asked May 24 '14 11:05

bigTree


2 Answers

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

like image 152
Sorcrer Avatar answered Oct 16 '22 04:10

Sorcrer


try this:

#include <stdio.h>

int main(){
    printf("EOF: %d\n", EOF);
}

EOF is not a printable char as you expected.

like image 26
mitnk Avatar answered Oct 16 '22 03:10

mitnk