First of all, ^Z or ^D are control characters that mean something to the terminal you are using, and sometimes that means for the terminal to signal end-of-file condition.
Anyway, your three keypresses are processed by the terminal to take the following actions, after entering text:
- Flush the input (i.e. send the characters that have been input so far from the terminal to the program – by default this doesn’t happen as the terminal uses line buffering)
- Set end-of-file condition
- Set end-of-file condition again
Inside your program that corresponds to:
- Nothing happens: even though
a
is received,fgets
keeps reading until end-of-file or newline fgets
completes because of end-of file. However it does not return NULL because characters were read,"a"
to be specific.- A bug in old versions of glibc causes
fgets
to try to read again, even though it previously reached end-of-file.fgets
completes because of end-of-file, and returnsNULL
because there were no characters read.