C exit() library function

The function exit terminates calling process normally. Before terminating a process, it performs the following operations:

  • Functions registered with atexit are called.
  • All streams/files are closed and flushed if buffered, and all files created with tmpfile are removed.
  • Control is returned to the calling(host) environment.

Function prototype of exit

void exit(int status);
  • status : This is the status code returned to the host environment.
    • If status is EXIT_SUCCESS or 0, it indicates successful exit.
    • If status is EXIT_FAILURE or nonzero, it indicates failure.

Return value of exit

NONE

C program using exit function

The following program shows the use of exit function to terminate execution of program before it's completion.

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

int main(){
    printf("Program start\n");
    /* Terminating program using exit */
    exit(0);
    printf("It won't get printed ever\n");  
    return 0;
}

Output
Program start