Quantcast
Channel: Catch Ctrl-C in C - Stack Overflow
Browsing latest articles
Browse All 10 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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


Catch Ctrl-C in C

How does one catch Ctrl+C in C?

View Article
Browsing latest articles
Browse All 10 View Live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>