abort
for Abnormal Program Termination in C
abort()
is a function which causes abnormal program termination (without cleaning up).
It is defined in header <stdlib.h>
.
void abort(void); // (until C11) _Noreturn void abort(void); // (since C11 and until C23) [[noreturn]] void abort(void) // (since C23);
It causes abnormal program termination unless SIGABRT
is being caught by a signal handler passed to signal and the handler does not return.
Functions passed to atexit()
are not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.
Notes
POSIX specifies that the abort()
function overrides blocking or ignoring the SIGABRT
signal.
Some compiler intrinsics, e.g. __builtin_trap
(gcc, clang, and icc) or __fastfail
/__debugbreak
(msvc), can be used to terminate the program as fast as possible.
Example
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *fp = fopen("data.txt","r"); if (fp == NULL) { fprintf(stderr, "error opening file data.txt in function main()\n"); abort(); } /* Normal processing continues here. */ fclose(fp); printf("Normal Return\n"); return 0; }