I got this program my friend sent me. And were both new at ubuntu and she asked me what will happen if we enter ctrl-c on the terminal

#include <signal.h>
#include <stdio.h>

void catch(int signo) {
    printf("Signal %d caught\n", signo);

int main() {
    int i;
    struct sigaction act;
    act.sa_flags = 0;
    act.sa_handler = catch;
    if (sigaction(SIGINT, &act, NULL) != 0) {
    for (i = 0; i < 100; ++i) {
        printf("sleep %d ....\n", i);
  • 1
    Why don’t you just try?
    – Melebius
    Apr 10, 2017 at 11:19
  • askubuntu.com/questions/890591/…
    – luv.preet
    Apr 10, 2017 at 11:50
  • compile, link, run, enter <ctrl-c> watch what happens. Apr 10, 2017 at 15:31
  • the posted code does not compile. it is missing the statement: #include <unistd.h> for the sleep() function it is missing the statement: #include <stdlib.h> for the exit() function Apr 10, 2017 at 15:36
  • for ease of readability and understanding, 1) consistently indent the code. indent after every opening brace '{'. unindent before every closing brace '}'. Suggest using 4 spaces for each indent level. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line Apr 10, 2017 at 15:37

2 Answers 2


What will happen is:

  • your terminal will send the SIGINT (interrupt) signal
  • your process will intercept the signal and pass its numeric value 2 to your catch function which will print Signal 2 caught
  • having intercepted and processed the signal, the program will continue with the main print/sleep loop

For example

sleep 0 ....
sleep 1 ....
sleep 2 ....
sleep 3 ....
sleep 4 ....
^CSignal 2 caught
sleep 5 ....
sleep 6 ....
sleep 7 ....

You will be able to kill the program by sending a SIGKILL signal e.g. by suspending the process using CtrlZ and then typing pkill myprog (where myprog is the name of your compiled executable version of the code).

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

See man 7 signal for more information on signals and signal handling.


Control+C (control character intr) sends SIGINT which will interrupt the application. Usually causing it to abort, but this is up to the application to decide.

Not the answer you're looking for? Browse other questions tagged or ask your own question.