Combining setjmp()/longjmp() and Signal Handling

Combining setjmp()/longjmp() and signal handling, we will be able to catch a signal and then jump to some other places for further or special processing rather than going back to the interrupted statement.

Here is a simple example. Click here to download a copy of this program.

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

jmp_buf  JumpBuffer;

void     INThandler(int);

void  main(void)
     signal(SIGINT, INThandler);

     while (1) {
          if (setjmp(JumpBuffer) == 0) {
             printf("Hit Ctrl-C at anytime ... \n");

void  INThandler(int  sig)
     char  c;

     signal(sig, SIG_IGN);
     printf("OUCH, did you hit Ctrl-C?\n"
            "Do you really want to quit? [y/n] ");
     c = getchar();
     if (c == 'y' || c == 'Y')
     else {
          signal(SIGINT, INThandler);
          longjmp(JumpBuffer, 1);