在功能参数用void的 [英] Use of void in function parameter

查看:144
本文介绍了在功能参数用void的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  

刚开始用C和我无法找到答案,这...

Just started with C and I can't find answer to this...

int foo() { }

int foo(void) { }

这应该我preFER为什么?

Which should I prefer and why?

请注意,这个问题还要为: INT主。它应该是: INT主 INT主要(无效)时,我不希望任何命令行参数

Note that this question also goes for: int main. Should it be: int main or int main(void) when I don't want any command line arguments.

推荐答案

的两种典型形式主要是,根据标准(见C99第5.1.2.2节。 2计划启动):

The two canonical forms of main are, according to the standard (see C99 section 5.1.2.2.2 "Program startup"):

int main (void);
int main (int argc, char *argv[]); // or equivalent such as char **argv

其他人则明确允许的,但这些都是需要的人。

Others are specifically allowed but those are the required ones.

至于 FN(无效) FN(),I $ P的preferred形式$ PFER前者,因为我想明确说明,有没有参数。

As to the preferred form between fn(void) and fn(), I prefer the former since I like to explicitly state that there are no parameters.

也有细微的差别。 C99 6.7.5.3节函数声明第10款规定:

There is also a subtle difference. C99 section 6.7.5.3 "Function declarators", paragraph 10, states:

void类型的未命名参数的特殊情况下,如在列表中的唯一项目
  指定函数没有参数。

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

这同一节第14段显示,唯一的区别:

Paragraph 14 of that same section shows the only difference:

的标识符列表声明仅该函数的参数的标识符。在一个函数声明是该函数的定义的一部分空列表指定函数没有参数。在函数声明,是不是该函数的定义的一部分空列表指定没有关于参数的数量和类型的信息提供。

An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.

在换句话说,这意味着相同的无效在函数定义,但没有的的意思是在一个独立的声明符(即原型)。 INT FN(); 表示有关参数没有任何信息还不得而知,但 INT FN(无效); 意味着没有参数​​。

In other words, it means the same as void in the function definition but does not mean that in a standalone declarator (i.e., the prototype). int fn(); means that no information on the parameters is yet known but int fn(void); means there are no parameters.

这意味着:

int fn();
int fn (int x) { return x; }
int main (void) { return fn(0); }

是有效的,但是:

is valid but:

int fn(void);
int fn (int x) { return x; }
int main (void) { return fn(0); }

不是

这篇关于在功能参数用void的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆