没有main()函数的C语言程序
有两种方法可以不添加main()
函数来运行C语言程序,第一种用#define
预处理指令,第二种是使用-nostartfiles编译选项;
使用#define预处理指令
这种方法严格来说不能说没有main()
函数;因为编译器在预处理阶段会将下列代码中的func替换成main再进行编译;
#include <stdio.h>
#define func main
void func(){
printf("Hello World");
}
使用-nostartfiles编译选项
在编译时使用-nostartfiles
编译选项时,编译器将避免标准链接;
#include <stdio.h>
void func(){
printf("hello world");
}
以上代码在编译时添加-nostartfiles编译选项也能成功编译并能成功运行。
gcc test.c -nostartfiles