.TH atexit() "" "" "General Function (libc)"
.PC "Register a function to be called when the program exits"
.B "#include <stdlib.h>"
\fBint atexit(void (\fIfunction\^\fB)
\fBvoid (*\fIfunction\^\fB)();
.PP
.B atexit()
registers one or more functions to be called when the program exits.
These registered functions
can, for example, perform clean-up beyond what is ordinarily
performed when a program exits.
.B atexit()
can register up to 32 functions.
.PP
.I function
points to the function to be called.
A registered function takes no arguments and returns nothing.
.PP
The functions that
.B atexit()
registers are called when the program exits normally, i.e., when the function
.B exit()
is called or when
.B main()
returns.
They are called in
.I reverse
order of registration.
.PP
.B atexit()
returns zero if
.I function
could be registered, and a value other than zero if it could not.
.SH Example
This example registers two functions to be executed upon exiting:
one displays a message, and the other
waits for the user to press a key before terminating.
.DM
#include <stdlib.h>
#include <stdio.h>
.DE
.DM
void
lastgasp()
{
	fprintf(stderr, "Type return to continue");
}
.DE
.DM
void
get1()
{
	getchar();
}
.DE
.DM
main()
{
	/* set up get1() as last exit routine */
	atexit(get1);
	/* set up lastgasp() as exit routine */
	atexit(lastgasp);
.DE
.DM
	/* exit, which invokes exit routines */
	exit(EXIT_SUCCESS);
}
.DE
.SH "See Also"
.Xr exit(), exit
.Xr libc libc
.br
\*(AS, \(sc7.10.4.2
