Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

exit — C23 termination function

`exit(status)` from `<stdlib.h>` causes normal program termination and does not return. It first calls `atexit` functions in reverse registration order, then flushes and closes streams, removes `tmpfile` files, and finally returns status to the host environment.

Version: C23

Syntax

[[noreturn]] void exit(int status);

exit(status) from <stdlib.h> causes normal program termination and does not return. It first calls atexit functions in reverse registration order, then flushes and closes streams, removes tmpfile files, and finally returns status to the host environment.

The example registers first and second, writes buffered body , and calls exit(EXIT_SUCCESS). Output body second first demonstrates reverse handler order and the later flush of standard output; there is no code path after exit.

EXIT_SUCCESS, zero, and EXIT_FAILURE select defined status categories, but their host representation is implementation-defined. A second exit, combining exit with quick_exit, or a longjmp that escapes a handler is UB. at_quick_exit handlers are not called; late registration follows C23’s special ordering rule.

Examples

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

static void first(void) { fputs("first\n", stdout); }
static void second(void) { fputs("second ", stdout); }

int main(void) {
    if (atexit(first) != 0 || atexit(second) != 0) return EXIT_FAILURE;
    fputs("body ", stdout);
    exit(EXIT_SUCCESS);
}

Result

body second first

The example registers `first` and `second`, writes buffered `body `, and calls `exit(EXIT_SUCCESS)`. Output `body second first` demonstrates reverse handler order and the later flush of standard output; there is no code path after `exit`.

Sources