[Previous] [Contents] [Next]

exit()

Exit the calling program

Synopsis:

#include <stdlib.h>

void exit( int status );

Library:

libc

Description:

The exit() function causes the calling program to exit normally. When a program exits normally:

  1. All functions registered with the atexit() function are called.
  2. All open file streams (those opened by fopen(), fdopen(), freopen(), or popen()) are flushed and closed.
  3. All temporary files created by the tmpfile() function are removed.
  4. The return status is made available to the parent process; status is typically set to EXIT_SUCCESS to indicate successful termination and set to EXIT_FAILURE or some other value to indicate an error.

Returns:

The exit() function doesn't return.

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] )
{
    FILE *fp;

    if( argc <= 1 ) {
        fprintf( stderr, "Missing argument\n" );
        exit( EXIT_FAILURE );
    }

    fp = fopen( argv[1], "r" );
    if( fp == NULL ) {
        fprintf( stderr, "Unable to open '%s'\n", argv[1] );
        exit( EXIT_FAILURE );
    }
    fclose( fp );
    exit( EXIT_SUCCESS );
    
    /*
     You'll never get here; this prevents compiler
     warnings about "function has no return value".
    */
    return EXIT_SUCCESS;
}

Classification:

ANSI

Safety:
Cancellation point No
Interrupt handler No
Signal handler Not applicable
Thread Yes

Caveats:

A strictly-conforming POSIX application cannot assume that the exit() function will be signal-safe on other platforms.

See also:

abort(), atexit(), _exit(), main()


[Previous] [Contents] [Next]