HOWTO · C++
exit(1) in C++
This article explores the usage of exit(1) in C++, focusing on how to effectively manage program termination and error handling. Learn how to implement the exit function, understand its significance, and discover best practices for using exit codes. Whether you're a beginner or an experienced developer, this comprehensive guide will enhance your understanding of C++ programming.
On this page
exit(1) conventionally ends a C++ program with a nonzero status, signaling failure to the calling environment. A zero status conventionally signals success. In C++, the function is std::exit, declared in <cstdlib>; use EXIT_SUCCESS and EXIT_FAILURE for the standard status choices. Returning from main is usually preferable because it allows local objects to clean up normally.
What std::exit Does in C++
The function std::exit takes an integer status and ends the program without returning to its caller. The familiar exit(1) spelling refers to the C library function; in C++, use the qualified name std::exit to make its namespace explicit. The constants EXIT_SUCCESS and EXIT_FAILURE express standard success and failure statuses without assuming a particular numeric value for failure.
Normal termination through std::exit invokes functions registered with std::atexit, destroys objects with static storage duration, and flushes and closes C streams. It does not unwind the current call stack, so automatic objects in the function that calls std::exit are not destroyed. This matters when those objects own resources that require destructor-based cleanup.
How to Use std::exit with a Status Code
Use std::exit(EXIT_SUCCESS) when a function outside main must end the whole program successfully, though returning a status from main is usually easier to reason about. Run this example:
#include <cstdlib>
#include <iostream>
int main() {
std::cout << "Operation completed" << '\n';
std::exit(EXIT_SUCCESS);
}
Operation completed
For a fatal condition that prevents useful recovery, report the problem and end with std::exit(EXIT_FAILURE). This standalone program uses --fail to select that path; without the argument it completes normally. The diagnostic is written to standard error, and the selected status tells the calling environment whether the operation succeeded:
#include <cstdlib>
#include <iostream>
#include <string_view>
int main(int argc, char* argv[]) {
if (argc > 1 && std::string_view(argv[1]) == "--fail") {
std::cerr << "Unable to continue" << '\n';
std::exit(EXIT_FAILURE);
}
std::cout << "Operation completed" << '\n';
return EXIT_SUCCESS;
}
What Runs During Normal Termination
The cleanup behavior is selective. Registered std::atexit functions run, objects with static storage duration are destroyed, and C streams are flushed and closed. Automatic objects in the function that calls std::exit are skipped because the call does not return through their scopes. Therefore, do not rely on local destructors to release a file, lock, or other resource when using std::exit; arrange cleanup first or return normally through the owning scope.
A status communicates the program’s result, but it does not describe the failure by itself. Print or log a useful diagnostic before terminating, and document any application-specific status values for callers. For portable success and failure conventions, use EXIT_SUCCESS and EXIT_FAILURE from <cstdlib>.
Registering Cleanup with std::atexit
Register a function with std::atexit when it must run during normal termination through std::exit or a return from main. Functions run in the reverse of their registration order. They should perform bounded cleanup and avoid depending on automatic objects in the function that initiated termination.
This standalone example registers two callbacks with distinct labels. Because they run in reverse registration order, release_cache runs before close_log:
#include <cstdlib>
#include <iostream>
void close_log() {
std::cout << "close log" << '\n';
}
void release_cache() {
std::cout << "release cache" << '\n';
}
int main() {
std::atexit(close_log);
std::atexit(release_cache);
std::cout << "main work" << '\n';
return EXIT_SUCCESS;
}
main work
release cache
close log
When to Return from main Instead
Returning from main is the usual choice when ordinary work has finished or a result can be passed back through the call chain. The returned integer becomes the program status, and local automatic objects in main are destroyed as the function returns. This also lets enclosing functions and destructors complete their normal cleanup.
Use std::exit when the program must terminate immediately from a deeper function and normal returns cannot carry the decision back conveniently. Before doing so, account for automatic objects that will be skipped. For errors that can be handled, return an error value or propagate an exception to a suitable boundary; reserve process-wide termination for cases where continuing is not appropriate.