The following program simply counts the number of key strokes until reaching EOF, and prints the count. It works as designed unless I press "Ctrl+Z" at some point, which actually resets (zeros out) the count. Why is this happening?
#include <stdio.h>
int main(){
char ch;
int cnt = 0;
while ((ch = getchar()) != EOF)
{
cnt ++;
}
printf("%d",cnt);
return 0;
}
Here, Ctrl+D activates EOF (note final count includes space):
But here, Ctrl+Z resets count to Zero:
And here, shows how count continues from zero after ctrl+z reset: