main 主函数执行完毕后,是否可能会再执行一段代码?
2006-03-25 14:16
344 查看
在网上看到朗讯的一道笔试题,题目是这样的:
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明?
答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行
int fn1(void), fn2(void), fn3(void), fn4 (void);
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main( void )
{ freopen("out.txt","w",stdout);
int fn1();
int fn2();
int fn3();
int fn4();
string str("zhanglin");
/*
The _onexit function is passed the address of a
function (func) to be called when the program
terminates normally. Successive calls to _onexit create
a register of functions that are executed in LIFO (last-in-first-out)
order. The functions passed to _onexit cannot take parameters.
*/
_onexit( fn1 );
_onexit( fn2 );
_onexit( fn3 );
_onexit( fn4 );
printf( "This is executed first./n" );
//system("PAUSE");
return 0;
}
int fn1()
{
printf( "next./n" );
return 0;
}
int fn2()
{
printf( "executed " );
return 0;
}
int fn3()
{
printf( "is " );
return 0;
}
int fn4()
{
printf( "This " );
return 0;
}
main 主函数执行完毕后,是否可能会再执行一段代码,给出说明?
答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行
int fn1(void), fn2(void), fn3(void), fn4 (void);
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main( void )
{ freopen("out.txt","w",stdout);
int fn1();
int fn2();
int fn3();
int fn4();
string str("zhanglin");
/*
The _onexit function is passed the address of a
function (func) to be called when the program
terminates normally. Successive calls to _onexit create
a register of functions that are executed in LIFO (last-in-first-out)
order. The functions passed to _onexit cannot take parameters.
*/
_onexit( fn1 );
_onexit( fn2 );
_onexit( fn3 );
_onexit( fn4 );
printf( "This is executed first./n" );
//system("PAUSE");
return 0;
}
int fn1()
{
printf( "next./n" );
return 0;
}
int fn2()
{
printf( "executed " );
return 0;
}
int fn3()
{
printf( "is " );
return 0;
}
int fn4()
{
printf( "This " );
return 0;
}
相关文章推荐
- main 主函数执行完毕后,是否可能会再执行一段代码,给出说明?
- main主函数执行完毕后,是否可能会再执行一段代码?
- C语言main()主函数执行完毕后是否会再执行一段代码
- main函数执行完毕后,是否可能会再执行一段代码?
- main主函数执行完毕后,是否可能会再执行一段代码
- main ()函数执行完后是否会再执行一段代码?
- 深入探讨:main函数执行完毕后,是否可能会再执行一段代码?
- main主函数执行完毕后,是否可能会再执行一段代码
- 怎样在main()函数退出之后再执行一段代码?
- main 主函数执行完毕后可以再执行其它代码
- main主函数执行完毕后,是否会再执行一段代码?
- 面试例题4—main函数执行完毕后,是否会再执行一段代码
- atexit(在main函数执行完毕后,是否可能再执行一段代码?)
- 在页面加载完毕时同时执行2段ajax代码 结果只能执行一段
- C++ main 函数 之前执行一段代码
- 一个问题 - 当你写一段代码时,你是否会考虑这份代码可能面临的各种执行环境
- Main和WinMain函数执行前,系统进行的运行时裤的执行代码分析
- 怎样让一段代码在主函数之前执行
- main 函数执行以前以及以后,分别还会执行什么代码?(转载)
- IOS 中 main()函数中UIApplicationMain后面的代码不执行...