I have this simple piece of code in a file named printftest.c
.
#include <stdio.h>
int main(){
int c, i;
i = 0;
while ((c = getchar()) != EOF)
++i;
printf("c = %d\n", c);
printf("i = %d\n", i);
}
Compilation and execution is done as follows (on Windows):
gcc printftest.c && a.exe
Terminal session looks like this:
gcc printftest.c && a.exe
c = -1
^C
Now when I give ctrl-c (keyboard interrupt) as input in the terminal only the first printf statement is executed. Why does this happen? I would expect to print both of the statements or none. Can anyone explain where execution stops exactly and why?