C at_quick_exit() Function

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

Definition

The C at_quick_exit() function registers a quick-termination callback. Each successful call adds one callback to the quick_exit() stack. The callback must return normally and should avoid unsafe assumptions about program state.

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. Success is reported by 0 and the callback is registered. Failure is a nonzero result; the callback must then be treated as unregistered. The examples below move from the direct call to an edge condition and a practical use.

Syntax

Start with this concrete at_quick_exit() call:

C
at_quick_exit(cleanup);
CallWhat it does
at_quick_exit(cleanup);Registers a quick-termination callback 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
int at_quick_exit(void (*function)(void));

Parameters

ParameterDescription
functionPointer to a no-argument, no-return function to run during quick_exit().

Return Value

Return ValueDescription
0Registration succeeded.
NonzeroThe callback could not be registered.

C Examples

Example 1 — Register one callback

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

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

static void cleanup(void){fputs("cleanup\n",stderr);fflush(stderr);}
int main(void){if(at_quick_exit(cleanup)!=0)return EXIT_FAILURE;fputs("main work\n",stderr);fflush(stderr);quick_exit(EXIT_SUCCESS);}

How It Works

  1. Step 1. Define a callback with no parameters and no return value.
  2. Step 2. Register it and check at_quick_exit() for zero.
  3. Step 3. Perform the program's ordinary work using explicitly flushed stderr for this visible quick-exit demonstration.
  4. Step 4. Normal quick-exit processing invokes the callback.

Example 2 — Observe reverse order

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

C
#include <stdio.h>
#include <stdlib.h>
static void one(void){fputs("one\n",stderr);fflush(stderr);} static void two(void){fputs("two\n",stderr);fflush(stderr);}
int main(void){if(at_quick_exit(one)||at_quick_exit(two))return EXIT_FAILURE;quick_exit(EXIT_SUCCESS);}

How It Works

  1. Step 1. Register the first callback.
  2. Step 2. Register the second callback after it.
  3. Step 3. Initiate the matching termination path.
  4. Step 4. Callbacks run last-registered first.

Example 3 — Handle registration failure

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

C
#include <stdio.h>
#include <stdlib.h>
static void done(void){fputs("done\n",stderr);fflush(stderr);}
int main(void){int rc=at_quick_exit(done);if(rc!=0){fputs("not registered\n",stderr);return EXIT_FAILURE;}fputs("registered\n",stderr);fflush(stderr);quick_exit(EXIT_SUCCESS);}

How It Works

  1. Step 1. Capture the registration result.
  2. Step 2. Treat nonzero as a real failure.
  3. Step 3. Continue only after zero confirms registration.
  4. Step 4. The selected termination path runs the confirmed callback.

Frequently Asked Questions

at_quick_exit() registers a quick-termination callback. Each successful call adds one callback to the quick_exit() stack. The callback must return normally and should avoid unsafe assumptions about program state.
Include <stdlib.h> before calling at_quick_exit().
Success is reported by 0 and the callback is registered. Failure is a nonzero result; the callback must then be treated as unregistered.
Callbacks run only for quick_exit(), in reverse registration order. They do not run for exit(), _Exit(), abort(), or return from main.
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: atexit()

Continue through the C stdlib.h tutorials.

atexit() 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