尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

C 语言 int main() 和 int main(void) 的区别

C 语言 int main() 和 int main(void) 的区别 20260829更新文章C语言int main()和int main(void)的区别在 C 语言中经常会看到int main()、int main(void)、void main()、main()等写法。它们看起来差别不大但在 C99/C11/C17 这类传统 C 标准中含义并不完全相同。首先看标准。C99ISO/IEC 9899:1999在5.1.2.2.1 Program startup中明确给出了main的常见标准形式The function called at program startup is namedmain. It shall be defined with a return type ofintand with no parameters:intmain(void){/* ... */}or with two parameters:intmain(intargc,char*argv[]){/* ... */}or equivalent; or in some other implementation-defined manner.因此在标准 hosted C 环境中不需要命令行参数时推荐直接写intmain(void){return0;}需要命令行参数时则写intmain(intargc,char*argv[]){return0;}关键区别其实在()和(void)。C99 的6.7.5.3 Function declarators中明确规定The special case of an unnamed parameter of typevoidas the only item in the list specifies that the function has no parameters.也就是说intfunc(void);表示这个函数明确没有参数。而传统 C 中intfunc();含义并不是“函数可以接收任意参数”而是这个声明没有提供参数个数和参数类型的信息。C99 同一章节也明确指出空参数列表在不是函数定义的情况下no information about the number or types of the parameters is supplied.所以在 C99/C11/C17 中intfunc();和intfunc(void);并不完全等价。举个最直观的例子intfunc();intmain(void){func(123);return0;}这里func()没有完整描述参数信息因此编译器无法像完整函数原型那样严格检查参数。如果改成intfunc(void);intmain(void){func(123);// 参数不匹配return0;}func(void)已经明确说明函数没有参数所以调用func(123)时编译器能够直接发现问题。这也解释了以前常见的这个例子intmain(){if(0)main(42);return0;}而intmain(void){if(0)main(42);return0;}第二种写法会得到更明确的参数不匹配诊断。这里不能简单理解为“main()可以传参数”真正原因是传统 C 中空括号形式没有提供完整的参数原型信息。WG14 的Defect Report #317也专门讨论过空括号函数定义的问题并确认这种形式不会像(void)那样形成完整的无参数函数原型。因此从接口描述和编译器检查角度来说func(void)明显比func()更严谨。实际上C99 的6.11.6 Function declarators已经把空括号函数声明列为obsolescent feature过时特性。所以在现代 C 工程中无参数函数更推荐写voiduart_init(void);voidgpio_init(void);intmain(void);而不是voiduart_init();voidgpio_init();intmain();再说void main()。虽然一些老编译器、单片机编译器或特定运行环境允许voidmain(void){}但对于标准 hosted C 程序C99 明确规定main返回int所以更标准、可移植的写法仍然是intmain(void){return0;}其中return 0通常表示程序正常结束。至于main(){}这是早期 C 语言留下来的写法。旧标准曾允许省略返回类型并默认按int处理也就是所谓的implicit int。但从 C99 开始这种隐式int已经被取消因此现代 C 代码不应再这样写。另外要注意 C 和 C 的区别。在 C 中intfunc();和intfunc(void);都表示函数没有参数因此int main()和int main(void)在 C 中基本等价但在 C99/C11/C17 中不能直接套用这个结论。还有一个新变化从C23开始旧式空括号函数声明被彻底淘汰int func()现在也表示“无参数”语义终于和int func(void)统一了。因此可以简单理解为C99 / C11 / C17 func() 参数信息未指定 func(void) 明确没有参数 C23 func() 没有参数 func(void) 没有参数不过目前大量 MCU、嵌入式项目仍然使用 C99/C11/C17因此实际工程中继续写intmain(void)依旧是最明确、最稳妥的选择。总结对于目前常见的 C99/C11/C17int main(void)明确表示没有参数int main()旧式空参数形式没有完整提供参数信息void main()不是标准 hosted C 推荐形式main()依赖早期隐式int现代 C 不应再使用。因此没有命令行参数时直接使用intmain(void){return0;}即可。参考资料ISO/IEC 9899:19995.1.2.2.1 Program startupISO/IEC 9899:19996.7.5.3 Function declaratorsISO/IEC 9899:19996.11.6 Function declaratorsWG14 N1256https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdfWG14 Defect Report #317https://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_317.htmWG14 N2841C23 相关修改https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2841.htm20250121前文章在C语言编程中通常会看到 **int main()、int main(void)、void mainvoid、main和mainvoid、int main() 和 void首先来看看标准的主函数写法是什么样的在C99标准(ISO/IEC 9899:1999)的5.1.2.2.1 Program startup一节是这么描述的:The function called at program startup is namedmain. The implementation declares noprototype for this function. It shall be defined with a return type ofintand with noparameters:int main(void) { /…/ }or with two parameters (referred to here asargcandargv, though any names may beused, as they are local to the function in which they are declared):*int main(int argc, charargv[]) { /…/ }or equivalent;or in some other implementation-defined manner.在程序启动时调用的函数是namedmain。该实现声明了这种函数的含有型。它应使用intindand的返回类型定义没有 参数 int mainvoid{/ ... /} 或者使用两个参数参考ASARGCandargv尽管可以享受任何名称因为它们是所声明的函数的本地功能 * int mainint argcchar argv []{/ ... /} 或等同物;或以其他一些实现定义的方式。 C89标准的main(函数是可接受的尽管现在建议是使用C99的标准。**C99**标准只定义了如下两种可接受的函数原型int main(void)int main(int argc, char *argv[])main() 这些用法一直没有深入了解过。下面简单介绍一下各个的用法// 这样是正确的intmain(){if(0)main(42);}// 这样会出错intmain(void){if(0)main(42);}在C中 int main() 和 int main(void) 是等效的但在C语言中让括号空着代表编译器对是否接受参数保持沉默。在C 语言中 main() 省略返回类型也就相当说明返回类型为 int 型不过这种用法在C中逐渐被淘汰。虽然 void main在很多系统都适用但他毕竟不是标准的所以应该避免这种用法,应该使用这种 int main(void)的写法比较妥当。main和mainvoid只声明 main 的话系统默认为 int main。void main 无论何时都不应该使用因为主函数必须有返回值表明程序运行状态这两个main函数都不能接受参数。main这种写法在C99标准以前的版本中使用int main(void)指的是此函数的参数为空不能传入参数如果你传入参数就会出错。int main()表示可以传入参数。而在C99标准之后如果main函数不需要接受参数的情况下会写做mainvoid。void main():C语言从来没声明过void main()只声明过main()。抛弃一切用void main编写C程序的习惯参考链接和可深入理解C的其它链接main函数_百度百科http://baike.baidu.com/link?urlQPL_sSuIxACVYcdTzY43iXtxLGWUPY8inpjL2hZZPFscJZehGeHPUlqf_qG2xPtKKY-tlZPrm78Rsw3TwM-D7a#4_3 C99_百度百科http://baike.baidu.com/link?urlwGdu4AOAT462i_loNUmdUSqMYXoE-Dur0qQSuHRkXbdy-ib-wMnLp7rrJIwhh_tkf1fICH4JcUkMZ_4n_yIOy_ void main还是int main_百度贴吧**http://tieba.baidu.com/p/2085028518 int main(int argc,char* argv[])详解http://www.cnblogs.com/avril/archive/2010/03/22/1691477.html
返回列表