...
Calling exit()
causes normal program termination to occur. Other than returning from main()
, calling exit()
is the typical way to end a program. The function takes one argument of type int
, which should be either EXIT_SUCCESS
or EXIT_FAILURE
, indicating successful or unsuccessful termination, respectively. The value of EXIT_SUCCESS
is guaranteed to be zero. C99 Section, 7.20.4.3 says, "If the value of status is zero or EXIT_SUCCESS
, an implementation-defined form of the status successful termination is returned." The exit()
function never returns.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h> /* ... */ if (/* something really bad happened */) { exit(EXIT_FAILURE); } |
...
For example, calling
Code Block | ||||
---|---|---|---|---|
| ||||
atexit(turn_gizmo_off); |
registers the turn_gizmo_off()
function so that a subsequent call to exit()
will invoke turn_gizmo_off()
as it terminates the program. C99 requires that atexit()
can register at least 32 functions.
...
Returning from main()
causes normal program termination to occur. This is the preferred way to terminate a program. Evaluating the return
statement has the same effect as calling exit()
with the same argument.
Code Block | ||||
---|---|---|---|---|
| ||||
int main(int argc, char **argv) { /* ... */ if (/* something really bad happened */) { return EXIT_FAILURE; } /* ... */ return EXIT_SUCCESS; } |
...
Wiki Markup |
---|
<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:macro-id="071635e398f7194b-ea2b6e57-4d8049c3-841ea9b0-a33a4bfefae9a86ad8bb8e9e"><ac:parameter ac:name="">1</ac:parameter></ac:structured-macro>\[1\] Note that POSIX ^®^ strengthens the specification for {{_Exit()}} by prohibiting the function from flushing stream buffers. See the {{[documentation|http://www.opengroup.org/onlinepubs/9699919799/functions/_Exit.html]}} of the function in \[[IEEE Std 1003.1-2008|AA. Bibliography#IEEE Std 1003.1-2008]\]. |
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h> /* ... */ if (/* something really bad happened */) { _Exit(EXIT_FAILURE); } |
...
Calling abort()
causes abnormal program termination to occur unless the SIGABRT
signal is caught and the signal handler calls exit()
or _Exit()
.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h> /* ... */ if (/* something really bad happened */) { abort(); } |
...
Wiki Markup |
---|
<ac:structured-macro ac:name="anchor" ac:schema-version="1" ac:macro-id="b4b0712d9e7482da-32d3b8a6-467d4109-a2898e2c-c0ec1773a270f1cafe016c56"><ac:parameter ac:name="">2</ac:parameter></ac:structured-macro>\[2\] Unlike in the case of {{\_Exit()}}, POSIX ^®^ explicitly permits but does not require implementations to flush stream buffers. See the {{[documentation|http://www.opengroup.org/onlinepubs/9699919799/functions/abort.html]}} of the function in \[[IEEE Std 1003.1-2008|AA. Bibliography#IEEE Std 1003.1-2008]\]. |
...
The abort()
function should not be called if it is important to perform application-specific cleanup before exiting. In this noncompliant code example, abort()
is called after data is sent to an open file descriptor. The data may or may not be written to the file.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h> #include <stdio.h> int write_data(void) { const char *filename = "hello.txt"; FILE *f = fopen(filename, "w"); if (f == NULL) { /* Handle error */ } fprintf(f, "Hello, World\n"); /* ... */ abort(); /* oops! data might not be written! */ /* ... */ return 0; } int main(void) { write_data(); return 0; } |
...
In this compliant solution, the call to abort()
is replaced with exit()
, which guarantees that buffered I/O data is flushed to the file descriptor and the file descriptor is properly closed.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <stdlib.h> #include <stdio.h> int write_data(void) { const char *filename = "hello.txt"; FILE *f = fopen(filename, "w"); if (f == NULL) { /* Handle error */ } fprintf(f, "Hello, World\n"); /* ... */ exit(EXIT_FAILURE); /* writes data & closes f. */ /* ... */ return 0; } int main(void) { write_data(); return 0; } |
...