ITeye博客

main函数结束后怎么继续执行一段代码?

runfeel · 2013-07-10 15:39 · Markdown

在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外执行的函数体

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。