Previous Page
Next Page

exit

Terminates the program normally

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

The exit( ) function ends the program and returns a value to the operating environment to indicate the program's final status. Control never returns from the exit( ) function.

Before terminating the program, exit( ) calls any functions that have been registered by the atexit( ) function (in LIFO order), closes any open files, and deletes any files created by the tmpfile( ) function.

The file stdlib.h defines two macros for use as arguments to exit( ): EXIT_SUCCESS and EXIT_FAILURE. If the argument is equal to one of these values, the program returns a corresponding system-specific value to the operating system to indicate success or failure. An argument value of 0 is treated the same as EXIT_SUCCESS. For other argument values, the value returned to the host environment is determined by the implementation.

Example

FILE *f_in, *f_out;

enum { X_OK = 0, X_ARGS, X_NOIN, X_NOOUT };

if ( argc != 3 ) {
  fprintf( stderr, "Usage: program input-file output-file\n");
  exit( X_ARGS );
}

f_in  = fopen(argv[1], "r");
if ( f_in == NULL ) {
  fprintf( stderr, "Unable to open input file.\n");
  exit( X_NOIN );
}
f_out = fopen(argv[2], "a+");
if ( f_out == NULL ) {
  fprintf( stderr, "Unable to open output file.\n");
  exit( X_NOOUT );
}

/* ... read, process, write, close files ... */

exit( X_OK );

See Also

_Exit( ), atexit( ), abort( )


Previous Page
Next Page