Chapter Contents

Previous

Next
exit

exit



Terminate Execution

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
ERRORS
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdlib.h>

void exit(int code);


DESCRIPTION

exit terminates the program after closing all files. The integer argument code is used in an implementation-defined way to indicate the results of execution. Usually, a code of EXIT_SUCCESS indicates successful execution, and a code of EXIT_FAILURE indicates an unsuccessful execution.


RETURN VALUE

Control does not return from exit .


ERRORS

User ABEND 1203 or 1209 may be issued by exit if memory management data areas have been overlaid.


PORTABILITY

exit is generally portable, although any meaning associated with the code argument is not. Some systems only treat the last 8 bits of the code as significant, so codes from 0 to 255 are recommended for maximum portability.

Many C implementations also support a routine named _exit to terminate execution without closing files. This routine is available only when USS under OS/390 is installed.


IMPLEMENTATION

exit is implemented as a longjmp to a target defined in the library routine that calls main . Therefore, it can be intercepted with blkjmp .

On IBM's 370 system, EXIT_SUCCESS is 0 and EXIT_FAILURE is 16. The exit code is used as an OS/390 or CMS return code. Under OS/390, a code that is not between 0 and 4095 is changed to 4095.


EXAMPLE

This example shows how to exit a program if it is not called with a valid input filename:

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

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

   if (argc > 1){
      f = fopen(argv[1],"r");
      if (f == NULL){
         fprintf(stderr,
                 "Can't open file \"%s\"\n", argv[1]);
         exit(EXIT_FAILURE);
      }
      fclose(f);
      puts("File successfully opened and closed.");
      exit(EXIT_SUCCESS);
   }
   else{
      fprintf(stderr,"No file specified.\n");
      exit(EXIT_FAILURE);
   }
}


RELATED FUNCTIONS

abort , atexit , blkjmp , coexit , _exit


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.