在main函数执行结束后,还想继续执行一段甚至多段代码,这就需要用_onexit注册一个函数,使该函数在main结束后执行。_onexit(funname);其中参数是一个函数名,且该函数无参数,返回值为int。看代码:
#include <iostream>
using namespace std;
int fun1()
{
cout<<"this is in func1 \n";
return 1;
}
int fun2()
{
cout<<"this is in func2 \n";
return 1;
}
void main()
{
_onexit(fun1);
_onexit(fun2);
cout<<"this is in main\n";
}
运行结果如下:
先执行main中的输出,跳过了注册的函数,main结束后逆序执行注册的main外执行的函数体