具有空参数列表的main是否与具有空参数列表的main不同? [英] Is main with parameter list of void different from main with an empty parameter list?
问题描述
Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?
什么是 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,
5.1.2.2.1程序启动
在程序启动时调用的函数称为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) { /* ... */ }
或
,带有两个参数(此处称为argc和argv,尽管有 可以使用名称,因为它们是它们所在的函数的本地名称 声明):
int main(int argc, char *argv[]) { /* ... */ }
或同等学历;
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;
或
以其他实现定义的方式.
in some other implementation-defined manner.
因此,在此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不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!