Code:
#include <stdio.h>
#define NEWLINE '\n'
#define SPACE ' '
int main(void)
{
int ch;
int count = 0;
while((ch = getchar()) != EOF)
{
if(ch != NEWLINE && ch != SPACE)
count++;
}
printf("There are %d characters input\n" , count);
return 0;
}
Question:
Everything works just fine, it will ignore spaces and newline and output the number of characters input to the screen (in this program I just treat comma, exclamation mark, numbers or any printable special symbol character like ampersand as character too) when I hit the EOF simulation which is
^z
.But there's something wrong when I input this line to the program. For example I input this:
abcdefg^z
, which means I input some character before and on the same line as^z
. Instead of terminating the program and print out total characters, the program would continue to ask for input.The EOF terminating character input only works when I specify
^z
on a single line or by doing this:^zabvcjdjsjsj
. Why is this happening?