getchar
returns an int
, not a char
. The distinction is important because it might return EOF
which is a value which cannot be a char.
You can't reliably test for EOF if you convert the return value of getchar
to a char
. The loop will terminate when you hit the character whose value is (char)EOF
.
Fix it by declaring ch
as an int
.