Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#include <stdlib.h>
__noreturn__ void exit(int status);
The exit() function:
Functions registered with atexit() are called in the reverse order of their registration. A function registered with atexit(), before an object obj1 of static storage duration is initialized, will not be called until obj1's destruction has completed. A function registered with atexit(), after an object obj2 of static storage duration is initialized, will be called before obj2's destruction starts.
Process termination in _exit() is equivalent to program termination in exit().
The argument status can have a value from 0 to 255 inclusive or be one of the macros EXIT_SUCCESS or EXIT_FAILURE. The value of EXIT_SUCCESS is defined in stdlib.h as 0; the value of EXIT_FAILURE is 8.
This function is also available to C applications in a stand-alone Systems Programming C (SPC) Environment.
In a POSIX C program, exit() returns control to the kernel with the value of status. The kernel then performs normal process termination.
POSIX-level thread cleanup routines are not executed. These includes cleanup routines created with pthread_cleanup_push() and destructor routines created with pthread_key_create().
Special behavior for C++: If exit() is called in a z/OS® XL C++ program, the program terminates without leaving the current block, and therefore destructors are not called for local (automatic) variables. Destructors for initialized static objects will be called in the reverse order of the completion of their constructors.
Functions registered with atexit() are called in the reverse order of their registration. A function registered with atexit(), before an object obj1 of static storage duration is initialized, will not be called until obj1's destruction has completed. A function registered with atexit(), after an object obj2 of static storage duration is initialized, will be called before obj2's destruction starts.
exit() returns no values.
exit() returns control to its host environment, with the returned value status.
For example, if program A invokes program B using a call to the system() function, and program B calls the exit() function, then program B returns to its host environment, which is program A.
/* This example flushes all buffers, closes any open files, and ends the
program if it cannot open the file myfile.
*/
#include <stdio.h>
#include <stdlib.h>
FILE *stream;
int main(void)
{
⋮
if ((stream = fopen("myfile.dat", "r")) == NULL)
{
printf("Could not open data file\n");
exit(EXIT_FAILURE);
}
}