C Reference ยท stdlib.h
C exit() and atexit() functions
Reference for C exit(), atexit(), quick_exit() and at_quick_exit(): end a program normally, flush streams, run cleanup handlers in reverse order, set the exit status.
exit() ends the program immediately and in an orderly way, from any function, not only from main. It runs the functions registered with atexit() in reverse order, flushes and closes every open stream, removes files made by tmpfile(), and hands the exit status to the operating system.
Returning from main does the same thing: return n; in main is equivalent to calling exit(n). Use exit() directly when a deep function discovers an unrecoverable problem.
#include <stdlib.h> _Noreturn void exit(int status); int atexit(void (*func)(void));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
_Noreturn void exit(int status); int atexit(void (*func)(void)); _Noreturn void quick_exit(int status); int at_quick_exit(void (*func)(void));
| Name | Description |
|---|---|
status |
0 or EXIT_SUCCESS for success, EXIT_FAILURE for failure; other values are implementation-defined, but 1 to 255 work on Linux and Windows. |
func |
A function taking no arguments and returning nothing, to be called at exit. |
Returns: exit and quick_exit never return. atexit returns 0 on success and a nonzero value if the handler could not be registered.
Errors / undefined behavior: at least 32 handlers can be registered; more may fail. Calling exit() from inside an atexit handler, or calling it twice (for instance from two threads), is undefined behavior. longjmp out of a handler is also undefined.
Usage notes
- What exit does, in order: call
atexithandlers (last registered first), flush and close all open streams, deletetmpfile()files, then terminate withstatus. - Locals are not destroyed. There are no destructors in C. Memory from
mallocis released by the operating system anyway, but resources the OS does not clean up (lock files, temporary files you created) need anatexithandler. exitversusreturn. Returning frommainruns the same sequence. In functions other thanmain,returnonly goes back to the caller, soexitis the way to stop the whole program.quick_exit(C11) skips the normal handlers and does not flush streams; it runs handlers registered withat_quick_exitinstead._Exitskips even those.- Handlers see global state only, since they take no arguments. Keep them short and make them safe to run when initialization was only partly finished.
- Status values: portable programs use
EXIT_SUCCESSandEXIT_FAILURE. On Unix only the low 8 bits reach the parent. - Threads:
exitfrom any thread ends the whole process while other threads may still run, so signal them to stop first when you can.