exit for Normal Program Termination with Cleaning Up

exit(int) causes normal program termination to occur.

void exit(int exit_code); // until C11
_Noreturn void exit(int exit_code); // since C11 and until C23
[[noreturn]] void exit(int exit_code); since C23

(Defined in header <stdlib.h>)

Several cleanup steps are performed:

Notes

The functions registered with at_quick_exit are not called.

The behavior is undefined if a program calls exit more than once, or if it calls exit and quick_exit.

The behavior is undefined if during a call to a function registered with atexit, the function exits with longjmp.

Returning from the the main() function, either by a return statement or by reaching the end of the function, executes exit(), passing the argument of the return statement (or ​0​ if implicit return was used) as exit_code.

Example

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

int main(void)
{
    FILE *fp = fopen("data.txt","r");
    if (fp == NULL)
    {
       fprintf(stderr, "error opening file data.txt in function main()\n");
       exit( EXIT_FAILURE );
    }
    fclose(fp);
    printf("Normal Return\n");
    return EXIT_SUCCESS ;
}