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.
#include <stdlib.h> _Noreturn void abort(void); _Noreturn void _Exit(int status);
Try it
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.
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
_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
fflushmay vanish withabort()or_Exit(). Print diagnostics tostderr, 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 callsabort(). DefiningNDEBUGremoves assertions entirely (seeassert).- Core dumps depend on the system settings (
ulimit -con Linux) and may be disabled. _Exitandfork: afterfork()a child that fails toexecshould call_Exit, notexit, so it does not flush copies of the parent's buffers a second time.quick_exitsits between the two: it runsat_quick_exithandlers but skips normal ones (seeexitandatexit).- Signals:
_Exitis async-signal-safe, so it is the correct way to leave a signal handler that has to end the program.exitandabortrules differ; check your platform. - Windows:
abort()may show a dialog box in debug builds and sets exit code 3.
Related
exitandatexit,assert,signal,fork- Lessons: Signals, Error handling