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

CServer Side ProgrammingProgramming

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("
Ctrl + C cannot terminate the program
");    fflush(stdout); } int main (){    signal(SIGINT, signalHandle);    while(!0)    return 0; }

Output

Ctrl + C cannot terminate the program
raja
Updated on 17-Jul-2020 11:12:32

Advertisements