C abort() Function

Beginner
7 min read
Updated: Nov 2026
3 examples
<stdlib.h>

Definition

The C abort() function terminates the program abnormally. It raises SIGABRT. A signal handler may intercept the signal, but if the signal is not caught or is ignored, termination proceeds as defined by the implementation.

This function is declared in <stdlib.h>. Its contract matters as much as its call: validate every required precondition, check any documented sentinel, and keep ownership or host effects clear in the surrounding code. There is no success return value because abort() never returns to its caller. Failure is not reported with a return value; abort() initiates abnormal termination. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete abort() call:

C
abort();
CallWhat it does
abort();Terminates the program abnormally using the concrete values shown.

Include <stdlib.h>. Also include headers that declare any types, limits, locale tools, or I/O functions used around the call.

Library declaration
C
void abort(void);

Parameters

ParameterDescription
Noneabort() takes no parameters.

Return Value

Return ValueDescription
Does not returnAbnormal termination is initiated by raising SIGABRT.

C Examples

Example 1 — Report before aborting

This complete program demonstrates a distinct part of the abort() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){fputs("fatal input\n",stderr);abort();}

How It Works

  1. Step 1. Write diagnostic text to stderr.
  2. Step 2. Call abort() for an unrecoverable condition.
  3. Step 3. SIGABRT is raised.
  4. Step 4. The function never reaches a normal return.

Example 2 — Show that later code is unreachable

This complete program demonstrates a distinct part of the abort() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
int main(void){puts("before");fflush(stdout);abort();puts("after");}

How It Works

  1. Step 1. Print the line that precedes termination.
  2. Step 2. Flush explicitly so the demonstration is visible.
  3. Step 3. abort() initiates abnormal termination.
  4. Step 4. The later puts() call is unreachable.

Example 3 — Contrast with atexit

This complete program demonstrates a distinct part of the abort() contract and checks the result or precondition that matters for the scenario.

C
#include <stdio.h>
#include <stdlib.h>
static void done(void){puts("atexit cleanup");}
int main(void){atexit(done);fputs("aborting\n",stderr);abort();}

How It Works

  1. Step 1. Register a normal-exit callback.
  2. Step 2. Emit a diagnostic before failure.
  3. Step 3. abort() takes the abnormal path.
  4. Step 4. Normal atexit processing is bypassed.

Frequently Asked Questions

abort() terminates the program abnormally. It raises SIGABRT. A signal handler may intercept the signal, but if the signal is not caught or is ignored, termination proceeds as defined by the implementation.
Include <stdlib.h> before calling abort().
There is no success return value because abort() never returns to its caller. Failure is not reported with a return value; abort() initiates abnormal termination.
abort() does not run functions registered with atexit(). Open streams are not guaranteed to be flushed, so it is not a substitute for orderly error handling.
The function is specified by the C standard library. Exact limits, locale behavior, environment behavior, allocation capacity, or host effects can still vary where relevant.

Next: abs()

Continue through the C stdlib.h tutorials.

abs() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful