问题描述
什么是 void ?有人提供一些示例,正确使用 void 吗?写void main (void)
或main()
有什么区别?
What is a void ? Anyone provide some examples, proper use of void ? And what is the difference when we write void main (void)
or main()
?
推荐答案
在C语言中,通常(void)
表示函数调用中不需要参数,而()
表示未指定数量的参数.
In C, in general, (void)
means no arguments required in function call, while ()
means unspecified number of arguments.
例如
void foo(void)
{
// body
}
void bar()
{
//body
}
在通话环境中,
foo(); // Correct
foo(1); // Incorrect
bar(); // Correct
bar(1); // Also correct
这是一般的解释.
但是对于main()而言,C99 Standard表示,
But for your case for main() , C99 Standard says that,
在程序启动时调用的函数称为main.这 实现没有为此函数声明任何原型.应该是 定义为int的返回类型,并且没有参数: int main(void) { /* ... */ }
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) { /* ... */ }
或
或
因此,在此void main(void)
中,返回类型应为int
.
So, in this void main(void)
return type should be int
.
最后,对于main()
,未指定返回类型,因此隐式返回类型为int
.
And at last , for main()
,return type is not given so implicitly return type would be int
.
这篇关于具有空参数列表的main是否与具有空参数列表的main不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!