| Program termination | |||||||||||||||||||||
| |||||||||||||||||||||
| Unreachable control flow | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Communicating with the environment | |||||||||||||||||||||
| |||||||||||||||||||||
| Memory alignment query | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Signals | |||||||||||||||||||||
| Signal types | |||||||||||||||||||||
| Non-local jumps | |||||||||||||||||||||
| Types | |||||||||||||||||||||
Defined in header <stdlib.h> | ||
void exit(int exit_code); | (until C11) | |
_Noreturnvoid exit(int exit_code); | (since C11) (until C23) | |
[[noreturn]]void exit(int exit_code); | (since C23) | |
Causes normal program termination to occur.
Several cleanup steps are performed:
exit_code is zero orEXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. Ifexit_code isEXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.Contents |
The functions registered withat_quick_exit are not called.
The behavior is undefined if a program callsexit more than once, or if it callsexit andquick_exit
The behavior is undefined if during a call to a function registered withatexit, the function exits withlongjmp.
Returning from thethe main function, either by areturn statement or by reaching the end of the function, executesexit(), passing the argument of the return statement (or0 if implicit return was used) asexit_code.
| exit_code | - | exit status of the program |
(none)
#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");returnEXIT_SUCCESS;}
Possible output:
error opening file data.txt in function main()
| causes abnormal program termination (without cleaning up) (function)[edit] | |
| registers a function to be called onexit() invocation (function)[edit] | |
(C11) | causes normal program termination without completely cleaning up (function)[edit] |
C++ documentation forexit | |