#include <stdlib.h>
int atexit( void (*func)(void) );
其中,atexit函数的参数是一个函数地址,当调用此函数时无需向他传递任何参数,也不期望他返回一个值。exit调用这些函数的顺序与他们登记时候的顺序相反。同一函数如若登记多次,则也会被调用多次。
2 实例
#include <stdio.h>
static void my_exit1(void);
static void my_exit2(void);
static void my_exit3(void);
int main(void)
{
if(atexit(my_exit2)!=0)
printf("can't register my_exit2");
if(atexit(my_exit1)!=0)
printf("can't register my_exit1");
if(atexit(my_exit3)!=0)
printf("can't register my_exit1");
printf("main is done\n");
return 0;
}
static void my_exit1(void)
{
printf("first exit handler\n");
}
static void my_exit2(void)
{
printf("second exit handler\n");
}
static void my_exit3(void)
{
printf("third exit handler\n");
}
3 结果
main is done
third exit handler
first exit handler
second exit handler