Computer >> Computer tutorials >  >> Programming >> C programming

Write a program that does not terminate when Ctrl+C is pressed in C


In this problem, we have to create a program that does not terminate when ctrl+C is pressed. Instead, it prints

“Ctrl + C cannot terminate the program”.

For this, we can use signal handling. The signal SIGINT is created on pressing ctrl+c. To solve this problem, we will catch this signal and handle it.

Program to show the implementation of our solution,

Example

#include <stdio.h>
#include <signal.h>
void signalHandle(int sig_num) {
   signal(SIGINT, signalHandle);
   printf("\n Ctrl + C cannot terminate the program\n");
   fflush(stdout);
}
int main (){
   signal(SIGINT, signalHandle);
   while(!0)
   return 0;
}

Output

Ctrl + C cannot terminate the program