这个函数可以实现在main主函数执行完毕之后,才执行的代码。
(1)使用格式:_onexit(int fun()) ,其中函数fun()必须是带有int类型返回值的无参数函数;
(2)_onexit() 包含在头文件cstdlib中,cstdlib为c语言中的库函数;
(3)无论函数_onexit() 放到main中任意位置,它都是最后执行。
#include <iostream>
#include <cstdlib>
using namespace std;
int func1(),func2(),func3();
int main(){
_onexit(func2);
_onexit(func1);
_onexit(func3);
cout<<"First Line"<<endl;
cout<<"Second Line"<<endl;
}
int func1()
{
cout<<"fun1() executed!"<<endl;
return 0;
}
int func2()
{
cout<<"fun2() is"<<endl;
return 0;
}
int func3()
{
cout<<"fun3() This "<<endl;
return 0;
}