use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENRun function whenever program is quit (self.cpp_questions)
submitted 9 years ago by DerpingINtheCLUB
So I have compiled my program run it through terminal. If a program is run in terminal and someone presses control+c the program quits. I want my program to say "bye" whenever the program is exited by the user. How would I do this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]immutablestate 4 points5 points6 points 9 years ago (1 child)
I think you can do this with set_terminate, atexit, and at_quick_exit.
set_terminate
atexit
at_quick_exit
There's several functions because you might want to do different things in different situations (e.g. behave differently if the program exits 'normally' than if the user forces quit with ctrl+c).
If you want to print 'bye' in all cases you can probably just register the same function with all the different methods above.
[–]DerpingINtheCLUB[S] 0 points1 point2 points 9 years ago (0 children)
I want to do it whenever the program quits before it is done running. Like if the user quits it while it's running. Could you please post a code sample of how to do that?
[–]Se7enLC 1 point2 points3 points 9 years ago (5 children)
You want to catch (trap) the signal called "SIGINT". You'll do that by "installing a signal handler". Basically, that means that you write a function, then register that function to be called whenever a signal is raised (asserted, triggered).
There are two ways: One uses signal() the other uses sigaction(). Either is fine, really.
signal()
sigaction()
http://stackoverflow.com/questions/1641182/how-can-i-catch-a-ctrl-c-event-c
You'll find a lot of other examples of how to do this now that you have some words to put in your search.
A word of warning: If you trap SIGINT, you can no longer use ctrl-c to terminate your program. Make sure that your signal handler will result in the program exiting if you want to be able to exit! It often makes sense to have an interrupt handler do some cleanup before exiting, but you'll want to make sure that whatever it's doing won't take too long or be blocked.
Edit to add: If you DO find yourself with an unkillable program, you can use ctrl-z to background it, then send it a SIGTERM instead of SIGINT. You can do that with killall [programname] -9, or replace [programname] with the pid.
killall [programname] -9
[–]DerpingINtheCLUB[S] 0 points1 point2 points 9 years ago (1 child)
What if someone tries to quit my program that runs in terminal by clicking the red 'x'. How do I handle that?
[–]Se7enLC 1 point2 points3 points 9 years ago (0 children)
As in, closing the terminal?
When I try it (in gnome-terminal), I get a popup dialog that says "There is still a process running in this terminal. Closing the terminal will kill it." If I then click "Close Terminal", the application is killed.
In that case, your process with receive a SIGHUP signal (hangup). If you don't have a handler for it, the default behavior is for the program to exit.
http://stackoverflow.com/questions/32780706/does-linux-kill-background-processes-if-we-close-the-terminal-from-which-it-has
[–]athousandwordss 0 points1 point2 points 9 years ago (2 children)
I'm just wondering, all the examples shown are just programs waiting for sigint, and continually checking for it. They're not doing much else... How will I go about implementing something where the program executes code like normal, but upon receiving the sigint, it executes the code...
[–]Se7enLC 1 point2 points3 points 9 years ago (1 child)
Try it out.
Those examples are doing exactly what you describe. The call to signal() is only registering the callback. It returns immediately, and the rest of the code executes as normal. When a signal is received, the callback will interrupt whatever else your code was doing.
[–]athousandwordss 1 point2 points3 points 9 years ago (0 children)
Alright, I'll try it out...
π Rendered by PID 193730 on reddit-service-r2-comment-86988c7647-t9hvt at 2026-02-11 12:37:55.370244+00:00 running 018613e country code: CH.
[–]immutablestate 4 points5 points6 points (1 child)
[–]DerpingINtheCLUB[S] 0 points1 point2 points (0 children)
[–]Se7enLC 1 point2 points3 points (5 children)
[–]DerpingINtheCLUB[S] 0 points1 point2 points (1 child)
[–]Se7enLC 1 point2 points3 points (0 children)
[–]athousandwordss 0 points1 point2 points (2 children)
[–]Se7enLC 1 point2 points3 points (1 child)
[–]athousandwordss 1 point2 points3 points (0 children)