C/C++   main()主函数执行前和执行后还能运行其它的吗?

答案: 可以

例如:

#include<iostream>
#include<string>
using namespace std;
string str="预备......";
cout<<str<<endl;
int fn1()
{
 cout<<"中间"<<endl;
 return 0;
}
int main()
{
 cout<<"开始"<<endl;
 _onexit(fn1);
 cout<<"结束"<<endl;
 return 0;
}
全局变量在编译期间就已经执行了,而经过 _onexit注册的函数会在main()执行完后执行。  全局变量在main()函数调用之后,就开始分配内存;静态变量则在main()函数之前就已经初始化了。

运行结果:

什么能在main()函数之前或之后执行

相关文章:

  • 2021-08-22
  • 2022-12-23
  • 2021-08-17
  • 2022-12-23
  • 2021-11-27
  • 2022-01-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-22
  • 2021-05-24
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案