Ctrl-C maps to the ASCII control character ETX
(End-of-Text - 0x03), while Ctrl-Z maps to ASCII EOF
(End-of-Text - 0x26). There is also Ctrl-D for EOT
(End-of-Transmission - 0x04).
Now what a particular platform does when either of these are occur in the the stdin
stream is platform specific (and not C language or GCC specific). However the macro EOF
has integer value -1 (rather than ASCII value 0x03)
When your code is executed at https://onlinegdb.com/4bF8adsM4 :
- Ctrl-C : Program terminates immediately (causes SIGINT signal to terminate process)
- Ctrl-D :
a = -1
, getchar()
returns immediately.
- Ctrl-Z :
a == '\n'
`getchar() returns after newline (i.e. Ctrl-Z is ignored).
On Windows, (with Microsoft C in my tests):
- Ctrl-C :
a = -1
, getchar()
returns immediately, Program terminates after outputting "Program Ended\n"
with exception "Exception thrown at 0x7747B915 (KernelBase.dll) in scratch2.exe: 0x40010005: Control-C.
"
- Ctrl-D :
a == EOT
, getchar()
returns after newline
- Ctrl-Z :
a == -1
, getchar()
returns after newline
In the Ctrl-C case, the exception was shown in the debugger, in normal use (from the command line, nothing is reported). When run from the command line, it actually output "Finished\n"
as well. It seems that the process termination is asynchronous to the I/O and program execution and the program can continue to run for some time before being terminated. I added further text, and only some of it was output. YMMV
Either way, on Windows, Ctrl-Z on stdin
does trigger an EOF signal for the stdin
stream, while in Unix/Linux, it is Ctrl-D. In both cases Ctrl-C causes process termination by SIGINT, but on Windows at least, it is an asynchronous signal/exception, and the process may not terminate immediately. Also in Windows, the EOF after Ctrl-Z is buffered until after newline, whereas in Unix/Linux, it aborts the input immediately.
Note that it is possible to implement a SIGINT signal handler to trap Ctrl-C to prevent process termination by that method (or handle it more elegantly).
All the other ASCII control characters have Ctrl-key mappings, what any particular platform does with these and stdin
is also platform dependent.