Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Ctrl + Z and Ctrl + C

Tags:

c

gcc

eof

ctrl

As per my understanding Ctrl+Z means EOF, isn't it? When Ctrl+Z is passed in as an input in the following program;

#include <stdio.h>

int main() 
{
   int a;
   a = getchar();

   if(a == EOF)
       printf("Program Ended\n");

   printf("Finished");
   return 0;   
}

It prints;

Program Ended
Finished

which is totally reasonable.

But when Ctrl+C is passed in as an input, Why does it outputs; Program Ended, and simply end the program? Wasn't it supposed to end the program without printing anything as per the code?

PS: I'm using gcc 6.3.0

like image 514
A typical nerd Avatar asked Mar 11 '26 19:03

A typical nerd


1 Answers

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.

like image 99
Clifford Avatar answered Mar 13 '26 09:03

Clifford