while((ch = getchar()) != EOF)
ch should be declared as int - otherwise you could not distinguish EOF from the character with the code 0xFF