DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

atexit(S-osr5)


atexit -- register function to be called at termination

Syntax

cc . . . -lc

#include <stdlib.h>

int atexit(func) void (*func)(void);

Description

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 ``last in, first out.'' No more than 32 functions can be registered with atexit. The functions passed to atexit cannot take parameters.

Return value

atexit returns zero on success or -1 on failure.

See also

abort(S-osr5), exit(S-osr5)

Example

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

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

int fn1( ) { printf("next.\n"); }

int fn2( ) { printf("executed "); }

int fn3( ) { printf("is "); }

int fn4( ) { printf("This "); }


Output:

This is executed first.
This is executed next.

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.

Standards conformance

atexit is conformant with:

ANSI X3.159-1989 Programming Language -- C .


© 2005 System Services (S-osr5)
SCO OpenServer Release 6.0.0 -- 02 June 2005