gpt4 book ai didi

c - : main(){}, int main(){} 和 int main(void){} 有什么区别

转载 作者:行者123 更新时间:2023-11-30 19:54:41 26 4
gpt4 key购买 nike

我目前正在学习C,写过很多小程序。但是,我注意到主函数可以这样开始:

    main()
{
//code
}

    int main()
{
//code
return 0;
}

    int main(void)
{
//code
return 0;
}

我应该使用哪个选项?谢谢!

最佳答案

对于标准 C

For a hosted environment (that's the normal one), the C99 standard says:

5.1.2.2.1 Program startup

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[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

9) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char **argv, and so on.

这(在 C89 中有效)main()隐含的意思是(以前)int main(void) 。然而,默认返回类型规则在C99中已被放弃。另外:

main()意味着 - 一个函数 main接受未指定数量的参数。

main(void)表示“一个函数 main 不带参数。

关于c - : main(){}, int main(){} 和 int main(void){} 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com