Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C abort() and _Exit() functions

Reference for C abort() and _Exit(): stop a program without cleanup, what is skipped (atexit handlers, buffered output), core dumps and when to use them.

abort() ends the program abnormally by raising the SIGABRT signal. It does not run atexit handlers and does not promise to flush streams. On Unix it usually produces a core dump and the shell reports the process as killed by a signal. It is what assert() calls when an assertion fails.

_Exit() (C99) also ends the program at once without running handlers or flushing C streams, but it counts as a normal termination with the status you give. Use it only when ordinary cleanup would be wrong, for example in a child process after fork() fails to start a program.

C
#include <stdlib.h>

_Noreturn void abort(void);
_Noreturn void _Exit(int status);

Try it

C _Exit skips the buffer flush

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C abort ends with SIGABRT (expected to fail)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
_Noreturn void abort(void);
_Noreturn void _Exit(int status);
Name Description
status Exit status passed to the host environment by _Exit.

Returns: neither function returns. abort causes termination by the signal SIGABRT (on Unix the process is reported with status 128 + 6 = 134 by a shell, and the C-level status is "terminated by signal"). Whether open streams are flushed or temporary files removed is implementation-defined for abort and for _Exit.

Errors / undefined behavior: none to trigger; a SIGABRT handler may be installed with signal(), but if it returns, abort still terminates the program.

Usage notes

  • Buffered output can be lost. When stdout goes to a file or pipe it is fully buffered, so text printed without fflush may vanish with abort() or _Exit(). Print diagnostics to stderr, which is unbuffered, or flush first.
  • Choose abort() for bugs, exit(EXIT_FAILURE) for expected errors. Bad user input is an expected error; a violated internal invariant is a bug where a core dump is useful.
  • assert(expr) prints the failed expression and calls abort(). Defining NDEBUG removes assertions entirely (see assert).
  • Core dumps depend on the system settings (ulimit -c on Linux) and may be disabled.
  • _Exit and fork: after fork() a child that fails to exec should call _Exit, not exit, so it does not flush copies of the parent's buffers a second time.
  • quick_exit sits between the two: it runs at_quick_exit handlers but skips normal ones (see exit and atexit).
  • Signals: _Exit is async-signal-safe, so it is the correct way to leave a signal handler that has to end the program. exit and abort rules differ; check your platform.
  • Windows: abort() may show a dialog box in debug builds and sets exit code 3.