What is the c exit function ?

The exit function is part of the c standard library , and is defined in the stdlib.h header .

The stdlib.h define some types , macros , and it contains general utility functions , to perform numerical conversion , generate random numbers , perform sorting , allocate memory , or interact with the environment.

The exit function is used to interact with the environment . It has the following signature .

  1. void exit(int status);

The exit function is used to normally exit a program , it starts by calling all the functions which were registered with the atexit function .

The atexit function , takes a pointer to a function with no argument or return type . It returns 0 if the function was successfully registered to be called at program termination , when the exit function is called , or a non zero value if not . The atexit function has the following signature :

  1. int atexit(void (*func) (void) );

After calling the functions registered with the atexit function in reverse registration order , the exit function closes all open streams , causing them to be flushed . The files created by the tmpfile function are removed .

The tmpfile function is part of the stdio.h header , and is used to create temporary files , removed when closed , or upon program normal termination .

Next , if the passed status argument is 0 , or the integer valued macro : EXIT_SUCCESS , defined in the stdlib.h header , this means that a success status must be passed to the host environment , as such an implementation defined form of the success status , is returned to the hosting environment , to whom control is relinquished .

  1. /* exit_success.c */

  2. #include <stdlib.h>

  3. int main ( int argc , char* argv[] )
  4. {
  5.   exit(0);
  6.   /* Normally exit the program
  7.        with success status .
  8.      exit(EXIT_SUCCESS) could
  9.        also have been used .*/
  10. }

  11. /*
  12. $ cc exit_success.c
  13. # compile the program .

  14. $ ./a.out
  15. # execute the program .

  16. $ echo $?
  17. # check the exit status of
  18. #   the last program .
  19. 0
  20. # The exit status of the last
  21. #   program is 0 , which is
  22. #   success .*/

If the passed status argument , is the integer valued macro : EXIT_FAILURE , defined in the stdlib.h header , then the exit function , returns an implementation defined form , of the failure status to the hosting environment , to whom control is passed .

  1. /* exit_failure.c */

  2. #include <stdlib.h>

  3. int main ( int argc , char* argv[] )
  4. {
  5.   exit(EXIT_FAILURE);
  6.   /* Normally exit  the program
  7.        with failure status .*/
  8. }

  9. /*
  10. $ cc exit_failure.c
  11. # compile the program .

  12. $ ./a.out
  13. # execute the program .

  14. $ echo $?
  15. # check the exit status of
  16. #   the last program .
  17. 1
  18. # It is 1 , as such
  19. #  the exist status
  20. #  is failure .*/

For any other passed status values , the returned exit status , to the host environment , is implementation defined .

  1. /* exit_status_implementation.c */

  2. #include <stdlib.h>

  3. int main ( int argc , char* argv[] )
  4. {
  5.   exit(-1);
  6.   /* Normally exit the program ,
  7.        passing to the exit function
  8.        an exit status of -1 .
  9.      An implementation defined , exit
  10.        status is returned to
  11.        the hosing environment .*/
  12. }

  13. /*
  14. $ cc exit_status_implementation.c
  15. # compile the program .

  16. $ ./a.out
  17. # execute the program .

  18. $ echo $?
  19. # check the exit status of the
  20. #   last program .
  21. 255
  22. # The exist status is 255 .*/

The behavior of multiple calls to the exit function is undefined .