Is there something special about the name main or is it arbitrary?个
C程序入口点的名称"main"(在hosted environment中)是not任意的,但由C Standard:
5.1.2.2.1 Program startup
1 The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here asargc
andargv
, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
In C can the main function be called something else?个
大多数用于编译/构建C程序do的主流工具链都提供了为入口点过程使用其他名称的方法:通常,链接器选项显式地指定名称as outlined here.然而,这样的程序strictly不符合C标准,并且可能导致可移植性问题,如所引用的C标准草案的"Annexe J"所示:
J.3 Implementation-defined behavior
…
J.3.2 Environment
…
— The name and type of the function called at program startup in a freestanding environment
— An alternative manner in which themain
function may be defined