The C exit() function ends the program normally. Registered atexit callbacks run in reverse order, buffered C streams are flushed and closed, and control is returned to the host environment with the supplied status.
This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. There is no success return; EXIT_SUCCESS communicates successful termination to the host. There is no failure return; EXIT_FAILURE communicates an unsuccessful program result. The examples below move from the direct call to an edge condition and a practical use.
Syntax
Start with this concrete exit() call:
C
exit(EXIT_SUCCESS);
Call
What it does
exit(EXIT_SUCCESS);
Ends the program normally using the concrete values shown.
Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.
Library declaration
C
void exit(int status);
Parameters
Parameter
Description
status
Termination status, commonly EXIT_SUCCESS or EXIT_FAILURE.
Return Value
Return Value
Description
Does not return
Normal program termination begins.
C Examples
Example 1 — Terminate with success
This complete program demonstrates a distinct part of the exit() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void){puts("work complete");fflush(stdout);exit(EXIT_SUCCESS);}
Output
work complete
How It Works
Step 1. Finish the demonstration work.
Step 2. Flush stdout explicitly so every termination style displays it.
Step 3. Call exit() with EXIT_SUCCESS.
Step 4. Control does not return to main.
Example 2 — Terminate with failure
This complete program demonstrates a distinct part of the exit() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
int main(void){fputs("operation failed\n",stderr);exit(EXIT_FAILURE);}
Output
operation failed
How It Works
Step 1. Detect or simulate an unrecoverable program result.
Step 2. Write the explanation to stderr.
Step 3. Pass EXIT_FAILURE to exit().
Step 4. The host receives an unsuccessful termination status.
Example 3 — Observe the cleanup policy
This complete program demonstrates a distinct part of the exit() contract and checks the result or precondition that matters for the scenario.
C
#include <stdio.h>
#include <stdlib.h>
static void done(void){puts("handler ran");}
int main(void){atexit(done);puts("terminating");fflush(stdout);exit(EXIT_SUCCESS);}
Output
terminating
handler ran
How It Works
Step 1. Register a matching handler.
Step 2. Print the pre-termination line and flush it.
Step 3. Invoke exit().
Step 4. The matching callback runs before termination.
Frequently Asked Questions
exit() ends the program normally. Registered atexit callbacks run in reverse order, buffered C streams are flushed and closed, and control is returned to the host environment with the supplied status.
Include <stdlib.h> before calling exit().
There is no success return; EXIT_SUCCESS communicates successful termination to the host. There is no failure return; EXIT_FAILURE communicates an unsuccessful program result.
exit() calls atexit handlers and flushes and closes C streams. Do not call it when _Exit()-style immediate termination is required.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.