Home > Backend Development > C++ > body text

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

王林
Release: 2023-09-03 12:49:07
forward
1079 people have browsed it

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;
}
Copy after login

Output

Ctrl + C cannot terminate the program
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template