I was practicing C program that i read from The C ANSI Programming Language Second Edition
I learn about getchar()
function.And the book say that getchar()
read the character from stdin until newline character or EOF
has been reached.And in the book i try to rewrite the program to count character using getchar()
function.The code is fine when compiling. The problem is the code unable to display the long of character.
Here is the code:
#include <stdio.h>
int main (void){
long nc;
nc=0;
while(getchar()!=EOF)
nc++;
printf("%ld\n",nc);
return 0;
}
But when i change the EOF
to newline character \n
the code working as i expected but only
display the long of character only one line. After that the code terminate.
My question is what is EOF
exactly is and what is the difference between EOF
and newline character.
Is there other way to fix this program?