#includevoidsignalHandle(intsig_num){ signal("/> #includevoidsignalHandle(intsig_num){ signal(">

Home  >  Article  >  Backend Development  >  Write a program in C that does not terminate when Ctrl+C is pressed

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

王林
王林forward
2023-09-03 12:49:071090browse

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

In this problem we need to create a program that will not terminate when ctrl C is pressed. Instead it prints

"Ctrl C cannot terminate program".

For this, we can use signal processing. Pressing ctrl c creates signal SIGINT. To solve this problem, we will catch and handle this signal.

Program showing the implementation of our solution:

Example

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

Output

Ctrl + C cannot terminate the program

The above is the detailed content of Write a program in C that does not terminate when Ctrl+C is pressed. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete