C _Exit() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C _Exit() function terminates without normal cleanup. It returns control to the host environment immediately. Unlike exit(), it bypasses registered normal cleanup and C stream flushing.

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 can communicate successful immediate termination. 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_FAILURE);
CallWhat it does
_Exit(EXIT_FAILURE);Terminates without normal cleanup 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

ParameterDescription
statusTermination status returned to the host environment.

Return Value

Return ValueDescription
Does not returnThe program terminates immediately.

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);}

How It Works

  1. Step 1. Finish the demonstration work.
  2. Step 2. Flush stdout explicitly so every termination style displays it.
  3. Step 3. Call _Exit() with EXIT_SUCCESS.
  4. 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);}

How It Works

  1. Step 1. Detect or simulate an unrecoverable program result.
  2. Step 2. Write the explanation to stderr.
  3. Step 3. Pass EXIT_FAILURE to _Exit().
  4. 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);}

How It Works

  1. Step 1. Register a normal-exit handler.
  2. Step 2. Print the pre-termination line and flush it.
  3. Step 3. Invoke _Exit().
  4. Step 4. Immediate termination skips the atexit callback.

Frequently Asked Questions

_Exit() terminates without normal cleanup. It returns control to the host environment immediately. Unlike exit(), it bypasses registered normal cleanup and C stream flushing.
Include <stdlib.h> before calling _Exit().
There is no success return; EXIT_SUCCESS can communicate successful immediate termination. There is no failure return; EXIT_FAILURE communicates an unsuccessful program result.
_Exit() does not call atexit handlers and does not guarantee flushing buffered streams. Flush explicitly first only when that is safe and intended.
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.

Next: free()

Continue through the C stdlib.h tutorials.

free() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful