C语言中总是从main函数开始执行函数吗?
来源:互联网 发布:window10安装不了软件 编辑:程序博客网 时间:2024/05/17 03:31
答案是否定的!
在gcc中,可以使用attribute关键字,声明constructor和destructor,代码如下:
[cpp] view plaincopy
#include <stdio.h>
__attribute((constructor)) void before_main()
{
printf("%s/n",__FUNCTION__);
}
__attribute((destructor)) void after_main()
{
printf("%s/n",__FUNCTION__);
}
int main( int argc, char ** argv )
{
printf("%s/n",__FUNCTION__);
return 0;
}
vc不支持attribute关键字,在vc中,可以使用如下方法:
[cpp] view plaincopy
#include <stdio.h>
int main( int argc, char ** argv )
{
printf("%s/n",__FUNCTION__);
return 0;
}
int before_main()
{
printf("%s/n",__FUNCTION__);
return 0;
}
int after_main()
{
printf("%s/n",__FUNCTION__);
return 0;
}
typedef int func();
#pragma data_seg(".CRT$XIU")
static func * before[] = { before_main };
#pragma data_seg(".CRT$XPU")
static func * after[] = { after_main };
#pragma data_seg()
编译执行,上述两段代码的结果均为:
before_main
main
after_main
可以在main前后调用多个函数,在gcc下使用attribute声明多个constructor、destructor,vc下在before、after数组中添加多个函数指针。
- c语言程序总是从main函数开始执行吗?
- C语言中总是从main函数开始执行函数吗?
- c程序从main函数开始执行?
- 为什么c语言从main函数开始
- 语言中如何在main函数开始前执行函数
- C语言中如何在main函数开始前执行函数
- C语言中如何在main函数开始前执行函数
- C语言中如何在main函数开始前执行函数
- C语言中如何在main函数开始前执行函数
- C语言中怎么在main函数开始前执行函数
- C语言中如何在main函数开始前执行函数
- 读“为什么c程序要从main函数开始执行”
- c 程序必须要从main函数开始执行么?
- c语言开始执行函数
- c语言中在main函数之前会执行什么
- C语言main函数执行的前后
- [ Python ] python 从哪开始执行 之 main 函数
- [ Python ] python 从哪开始执行 之 main 函数
- Servlet 执行不了
- oracle 11g 卸载 for win7
- String类的split() 方法中 关于转义字符的问题
- 关于软件测试的几个相关概念
- 写给24岁的自己
- C语言中总是从main函数开始执行函数吗?
- HDU 2509 Be The Winner ( Nim 博弈 )
- 工厂
- 输入数字n,按顺序输出从1最大的n位10进制数
- 编程能力强化(4)——模拟SQL语句解析
- UIElement.OnRenderSizeChanged
- 写给20岁的自己
- HDU2734:Quicksum
- 能用于 VC6 的 CImage 类