Answer by MCCCS for Catch Ctrl-C in C
@Peter Varo updated Dirk's answer, but Dirk rejected the change. Here's the new answer by Peter:Although the above snippet is a correct c89 example, one should use the more modern types and guarantees...
View ArticleAnswer by Love Bisaria for Catch Ctrl-C in C
#include<stdio.h>#include<signal.h>#include<unistd.h>void sig_handler(int signo){ if (signo == SIGINT) printf("received SIGINT\n");}int main(void){ if (signal(SIGINT, sig_handler) ==...
View ArticleAnswer by Filip J. for Catch Ctrl-C in C
Addendum regarding UN*X platforms.According to the signal(2) man page on GNU/Linux, the behavior of signal is not as portable as behavior of sigaction:The behavior of signal() varies across UNIX...
View ArticleAnswer by alemol for Catch Ctrl-C in C
This just print before exit.#include <stdio.h>#include <stdlib.h>#include <unistd.h>void sigint_handler(int);int main(void){ signal(SIGINT, sigint_handler); while (1){ pause(); }...
View ArticleAnswer by Clifford for Catch Ctrl-C in C
Regarding existing answers, note that signal handling is platform dependent. Win32 for example handles far fewer signals than POSIX operating systems; see here. While SIGINT is declared in signals.h on...
View ArticleAnswer by Walter for Catch Ctrl-C in C
Or you can put the terminal in raw mode, like this:struct termios term;term.c_iflag |= IGNBRK;term.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF);term.c_lflag &= ~(ICANON | ECHO | ECHOK | ECHOE |...
View ArticleAnswer by Paul Beckingham for Catch Ctrl-C in C
Set up a trap (you can trap several signals with one handler):signal (SIGQUIT, my_handler);signal (SIGINT, my_handler);Handle the signal however you want, but be aware of limitations and gotchas:void...
View ArticleAnswer by icyrock.com for Catch Ctrl-C in C
Check here:http://www.csl.mtu.edu/cs4411.ck/www/NOTES/signal/install.htmlNote: Obviously, this is a simple example explaining just how to set up a Ctrl+C handler, but as always there are rules that...
View ArticleAnswer by Dirk is no longer here for Catch Ctrl-C in C
With a signal handler. Here is a simple example flipping a bool used in main():#include <signal.h>static volatile int keepRunning = 1;void intHandler(int dummy) { keepRunning = 0;}// ...int...
View Article