exit() Terminate the program normally
exit() Terminate the program normally
exit() terminates normally without returning, flushing and closing streams. Use EXIT_SUCCESS or EXIT_FAILURE; unlike return, it exits the whole program.
| Category | stdlib.h utilities |
|---|---|
| Kind | Library function |
| Header | <stdlib.h> |
| C standard | C89 / C90 |
📝 Syntax
void exit(int status);
⚙️ Parameters
| status Required | A termination status for the host environment: 0 or EXIT_SUCCESS denotes success, and EXIT_FAILURE denotes failure. |
|---|
Returns: No value is returned because the function does not return.
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Before exit
⚠️ Warning exit() does not return to the call site, so subsequent statements are not executed. Do not call it when the current function must continue.
💡 Tip Normal termination calls functions registered with atexit() and flushes and closes open streams. Automatic local objects have no additional destructor mechanism in C.
🏷️ Related stdlib.h functions
📚 Related Tutorials
❓ FAQ
QWhat is the difference between exit and returning from main?
AReturning status from main is essentially equivalent to exit(status). However, exit can terminate the whole program from another function, whereas an ordinary return exits only the current function.
QDoes exit flush buffered printf output?
AYes. Normal termination through exit flushes and closes all open output streams, unlike abnormal termination or an abrupt process kill.
QIn what order does exit call functions registered with atexit?
AThey are called in reverse registration order, so the last function registered runs first. Normal termination also flushes and closes open streams; programs that require precise resource release should not rely solely on global exit cleanup.