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 .

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 :

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 .

/* exit_success.c */

#include <stdlib.h>

int main ( int argc , char* argv[] )
{
  exit(0);
  /* Normally exit the program
       with success status .
     exit(EXIT_SUCCESS) could 
       also have been used .*/
}

/*
$ cc exit_success.c
# compile the program .

$ ./a.out 
# execute the program .

$ echo $?
# check the exit status of 
#   the last program . 
0
# The exit status of the last
#   program is 0 , which is
#   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 .

/* exit_failure.c */

#include <stdlib.h>

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

/*
$ cc exit_failure.c
# compile the program .

$ ./a.out 
# execute the program .

$ echo $?
# check the exit status of 
#   the last program . 
1
# It is 1 , as such 
#  the exist status 
#  is failure .*/

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

/* exit_status_implementation.c */

#include <stdlib.h>

int main ( int argc , char* argv[] )
{
  exit(-1);
  /* Normally exit the program ,
       passing to the exit function
       an exit status of -1 .
     An implementation defined , exit
       status is returned to
       the hosing environment .*/
}

/*
$ cc exit_status_implementation.c
# compile the program .

$ ./a.out 
# execute the program .

$ echo $?
# check the exit status of the 
#   last program . 
255
# The exist status is 255 .*/

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