gpt4 book ai didi

c - int main() 和 int main(void) 之间的区别?

转载 作者:太空狗 更新时间:2023-10-29 16:17:47 26 4
gpt4 key购买 nike

以下是什么意思:

int main(void) {...} 

对比

int main() {...}

?

我认为 int main() {...} 意味着 main 没有接收任何参数(来自命令行),但是:

int main(int argc, char *argv[])

会。

但是,int main(void) {...} 是什么意思?还有,void 代表什么?

我看过 here但不知何故这是一个不同的问题。

最佳答案

在 C++ 中,没有区别。


在 C 语言中,差异值得怀疑。有些人喜欢争辩说后一个版本(没有 void 的那个)在技术上只是一个通用的实现扩展,并且由于标准中的措辞而不能保证按标准工作。但是,该标准明确指出,在函数定义中,一组空参数具有明确定义的行为:该函数不采用任何参数。因此 main 的这样一个定义符合标准中的以下描述:

It [main] shall be defined with a return type of int and with no parameters.

然而,两者之间有一个明显的区别:即没有 void 的版本无法为函数提供正确的原型(prototype):

// this is OK.
int main()
{
if (0) main(42);
}

// this requires a diagnostic to be shown during compiling
int main(void)
{
if (0) main(42);
}

哦,补充一下:void 在所有函数声明符中具有以下含义:

(6.7.6.3p10) The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

关于c - int main() 和 int main(void) 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12225171/

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