I was confused by the getchar() function, so I searched this great website and read all the relative topics and read about getchar() in K&R book. But when I ran the code, typed a word on the console screen, and hit enter, nothing happened. I expected the number of characters to be displayed.
#include<stdio.h>
int main(void)//doesn't work??
{
int c ;
int count ;
while ( ( c = getchar() ) != EOF )
count ++ ;
printf( "%d characters\n" , count ) ;
return 0;
}
I think its because you are pressing enter and expecting the loop to stop.
The condition
will only be false on Windows when you press
Ctrl + Z.On UNIX it is
Ctrl + DI think, so it should beCtrl + Dif you are using Ubuntu.If you want the loop to stop on pressing enter try checking for the
'\n'character instead.