关于函数参数(void与空参数)问题

1. 平时进行函数声明的时候,无参数的函数应该声明成f(void)

C99 P119:

10 The special case of an unnamed parameter of type void as the only item in the list

specifies that the function has no parameters  

----------------------------------------------

16: The declaration

int f(void), *fip(), (*pfi)();

declares a function f with no parameters returning an int, a function fip with no parameter specification

returning a pointer to an int, and a pointer pfi to a function with no parameter specification returning an

int.

-----------------------------------------------

不写void是K&R C标准中的, 但是现在,在任何一本书中都推荐使用void的写法 

C99: 6.7.5.3

Empty parameter lists 

C distinguishes between a function declared with an empty parameter list and

a function declared with a parameter list consisting of only void.

The former is an unprototyped function taking an unspecified number of arguments,

while the latter is a prototyped function taking no arguments. 

// C code

extern int  foo();          // Unspecified parameters

extern int  bar(void);      // No parameters

void baz()

{

foo(0);         // Valid C, invalid C++

foo(1, 2);      // Valid C, invalid C++

bar();          // Okay in both C and C++

bar(1);         // Error in both C and C++

2. K&R C标准,还可以在函数外定义函数输入参数

void fun(i)

int i;

{
 
printf("%d\n", i);
 
return ;
 
}
 
现在已经不推荐使用了,但大多数编译器仍能通过而不报任何错误或者警告
永不止步步 发表于01-12 11:20 浏览65535次
分享到:

已有0条评论

暂时还没有回复哟,快来抢沙发吧

添加一条新评论

只有登录用户才能评论,请先登录注册哦!

话题作者

永不止步步
金币:67417个|学分:383141个
立即注册
畅学电子网,带你进入电子开发学习世界
专业电子工程技术学习交流社区,加入畅学一起充电加油吧!

x

畅学电子网订阅号