CKTIS / C
atexit — C23 library function
`atexit(void (*func)(void))` from `<stdlib.h>` registers a no-argument function to be called during normal program termination. It returns zero on success and a nonzero value when registration fails.
Version: C23
Syntax
int atexit(void (*func)(void));atexit(void (*func)(void)) from <stdlib.h> registers a no-argument function to be called during normal program termination. It returns zero on success and a nonzero value when registration fails.
Use it for final process-resource cleanup that cannot conveniently follow local control flow. The example registers two distinct handlers, checks both results, and lets return from main invoke them as second and then first.
An implementation must support at least 32 registrations, and every successful registration—including repeated registration of the same function—produces one call. Order is normally reverse registration order, except that a handler registered during termination is called after any previously registered handlers already called when it was registered. Escaping a handler through longjmp has undefined behavior; abort, _Exit, and quick_exit do not run these handlers.
Examples
#include <stdio.h>
#include <stdlib.h>
static void first(void) { puts("first"); }
static void second(void) { puts("second"); }
int main(void) {
if (atexit(first) != 0 || atexit(second) != 0) return EXIT_FAILURE;
puts("body");
return EXIT_SUCCESS;
}
Result
body second first
Use it for final process-resource cleanup that cannot conveniently follow local control flow. The example registers two distinct handlers, checks both results, and lets return from `main` invoke them as `second` and then `first`.