Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Processes the specified function at exit.

int atexit(
   void (__cdecl *func )( void )


Function to be called.

Return Value

atexit returns 0 if successful, or a nonzero value if an error occurs.


The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in last-in, first-out (LIFO) order. The functions passed to atexit cannot take parameters. atexit and _onexit use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.


Routine Required header Compatibility
atexit <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.

To generate an ANSI-compliant application, use the ANSI-standard atexit function (rather than the similar _onexit function).


This program pushes four functions onto the stack of functions to be executed when atexit is called. When the program exits, these programs are executed on a last in, first out basis.

// crt_atexit.c
#include <stdlib.h>
#include <stdio.h>

void fn1( void ), fn2( void ), fn3( void ), fn4( void );

int main( void )
   atexit( fn1 );
   atexit( fn2 );
   atexit( fn3 );
   atexit( fn4 );
   printf( "This is executed first.\n" );

void fn1()
   printf( "next.\n" );

void fn2()
   printf( "executed " );

void fn3()
   printf( "is " );

void fn4()
   printf( "This " );


This is executed first.
This is executed next.

See Also

Process and Environment Control Routines | abort, exit | _onexit | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft