C atexit() Function

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

Definition

The C atexit() function registers a normal-termination callback. Callbacks are invoked by exit() and by a normal return from main. Multiple registrations are allowed up to an implementation-defined capacity.

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. Success is 0, meaning the callback will participate in normal termination. Failure is nonzero, so code must not assume that cleanup was registered. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete atexit() call:

C
atexit(cleanup);
CallWhat it does
atexit(cleanup);Registers a normal-termination callback 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
int atexit(void (*function)(void));

Parameters

ParameterDescription
functionPointer to a no-argument, no-return function to run during normal termination.

Return Value

Return ValueDescription
0Registration succeeded.
NonzeroThe callback could not be registered.

C Examples

Example 1 — Register one callback

This complete program demonstrates a distinct part of the atexit() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>

static void cleanup(void){puts("cleanup");}
int main(void){if(atexit(cleanup)!=0)return EXIT_FAILURE;puts("main work");return 0;}

How It Works

  1. Step 1. Define a callback with no parameters and no return value.
  2. Step 2. Register it and check atexit() for zero.
  3. Step 3. Perform the program's ordinary work using a normal stream.
  4. Step 4. Normal exit processing invokes the callback.

Example 2 — Observe reverse order

This complete program demonstrates a distinct part of the atexit() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
static void one(void){puts("one");} static void two(void){puts("two");}
int main(void){if(atexit(one)||atexit(two))return EXIT_FAILURE;return 0;}

How It Works

  1. Step 1. Register the first callback.
  2. Step 2. Register the second callback after it.
  3. Step 3. Initiate the matching termination path.
  4. Step 4. Callbacks run last-registered first.

Example 3 — Handle registration failure

This complete program demonstrates a distinct part of the atexit() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
static void done(void){puts("done");}
int main(void){int rc=atexit(done);if(rc!=0){fputs("not registered\n",stderr);return EXIT_FAILURE;}puts("registered");return 0;}

How It Works

  1. Step 1. Capture the registration result.
  2. Step 2. Treat nonzero as a real failure.
  3. Step 3. Continue only after zero confirms registration.
  4. Step 4. The selected termination path runs the confirmed callback.

Frequently Asked Questions

atexit() registers a normal-termination callback. Callbacks are invoked by exit() and by a normal return from main. Multiple registrations are allowed up to an implementation-defined capacity.
Include <stdlib.h> before calling atexit().
Success is 0, meaning the callback will participate in normal termination. Failure is nonzero, so code must not assume that cleanup was registered.
Registered callbacks run in reverse registration order during normal termination. They do not run after abort() or _Exit().
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: atof()

Continue through the C stdlib.h tutorials.

atof() 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