在C语言中,main函数可以调用其他函数吗?"Main"这个名字有什么特别之处,还是很随意?

推荐答案

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 as argc and argv, 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 the main function may be defined

C++相关问答推荐

如何将一个integer与一个数组进行比较?

海湾合作委员会是否保证大小匹配的访问?

segfault在C中使用getline()函数

与unions 的未定义行为

将fget()与strcMP()一起使用不是正确的比较

#If指令中未定义宏?

将宏值传递给ARM链接器,该链接器将变量放置在特定位置

struct -未知大小

在每种If-Else情况下执行语句的最佳方式

强制转换变量以在 struct 中蚕食

Boyer Moore算法的简单版本中的未定义行为

如何使用唯一数字对整型进行分区

为什么Linux无法映射这个PT_LOAD ELF段?

为什么我的半数组测试和奇数组测试不起作用?(我使用Assert进行调试)

如何使用calloc和snprintf

我正在使用 klib 库 我可以使用 (khash) KHASH_SET_INIT_INT64() 负值作为键.因为我在头文件中看到它使用 unsigned long int

C Makefile - 如何避免重复提及文件名

使用共享变量同步多线程 C 中的函数

malloc:损坏的顶部大小无法找出问题

int 与 size_t 与 long