exit() — End program

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
C11
Single UNIX Specification, Version 3

both  

Format

#include <stdlib.h>

__noreturn__ void exit(int status);

General description

The exit() function:

  1. Calls all functions registered with the atexit() function, and destroys C++ objects with static storage duration, all in last-in-first-out (LIFO) order. C++ objects with static storage duration are destroyed in the reverse order of the completion of their constructor. (Automatic objects are not destroyed as a result of calling exit().)

    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.

  2. Flushes all buffers, and closes all open files.
  3. All files opened with tmpfile() are deleted.
  4. Returns control to the host environment from the program.

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.

Returned value

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.

Example

/* 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);
      }
}

Related information