Format
#include <stdlib.h> onexit_t _onexit(onexit_t func);
Language Level: Extension
_onexit records the address of a function func to call
when the program ends normally. Successive calls to _onexit
create a stack of functions that run in a last-in, first-out
order. The functions passed to _onexit cannot take parameters.
You can record up to 32 termination functions with calls to _onexit and atexit. If you exceed 32 functions, _onexit returns the value NULL.
Note: For portability, use the ANSI/ISO standard atexit function, which is equivalent to _onexit.
Return Value
If successful, _onexit returns a pointer
to the function; otherwise, it returns a NULL value.
Example
This example specifies and defines four
distinct functions that run consecutively at the completion of
main.
#include <stdio.h> #include <stdlib.h>
int fn1(void)
{
printf("next.\n");
return 0;
}
int fn2(void)
{
printf("run ");
return 0;
}
int fn3(void)
{
printf("is ");
return 0;
}
int fn4(void)
{
printf("This ");
return 0;
}
int main(void)
{
_onexit((onexit_t)fn1);
_onexit((onexit_t)fn2);
_onexit((onexit_t)fn3);
_onexit((onexit_t)fn4);
printf("This is run first.\n");
return 0;
/***********************************
The output should be:
This is run first.
This is run next.
***********************************/
}
![]()
abort -- Stop a Program
atexit -- Record
Program Termination Function
exit -- End
Program
_exit -- End
Process
<stdlib.h>