[Previous]
[Contents]
[Next]

exit()

cause normal program termination to occur

Synopsis:

#include <stdlib.h>
void exit( int status );

Description:

The exit() function causes normal program termination to occur.

  1. The functions registered by the ANSI atexit() function or non-ANSI onexit() function are called in the reverse order of their registration.
  2. All open stream files (that is, those opened by fopen(), fdopen() or freopen()) are flushed and closed, and all files created by the tmpfile() function are removed.
  3. The return status is made available to the parent process. The status value is typically set to 0 to indicate successful termination, or set to some other value to indicate an error.

Returns:

The exit() function doesn't return to its caller.

Examples:

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

void 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 );
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

abort(), atexit(), _exit(), onexit()


[Previous]
[Contents]
[Next]