C exit()

The exit() function is defined in the stdlib.h header file. It helps to return to the operating system after terminating the program execution. Actually it returns from the current process without executing the rest of the codes.

void exit(int status); #where status indicates status value


exit() Parameters: 

The exit() function takes a single parameter. Here the status code indicates the error messages.

Parameter Description Required / Optional
status the status value returned to the parent process Required

exit() Return Value

The exit() function does not return any value. If the status argument is "0" exits a program without any error message. If other codes are given as status codes it indicates that the system can handle the error messages.

Examples of exit() 

Example 1: Working of exit() function in C?

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

int main()
    printf("Program is Started....\n");
   printf("Program is Exiting....\n");

   printf("Program reaches to End....\n");



Program is Started....
Program reaches to End....

Example 2: How exit() works in error case?

#include <stdio.h>
#include <stdlib.h>
int main (){
  FILE *p = fopen("mydata.txt","r");
    if (p == NULL) {
       fprintf(stderr, "Opening file mydata.txt have error in function main()\n");
       exit( EXIT_FAILURE );
    printf("Normal Return\n");
    return EXIT_SUCCESS ;


Opening file mydata.txt have error in function main()