关于函数参数(void与空)


  1. #include <stdio.h>;
  2. int f();

  3. int main()
  4. {
  5.         int i=10;
  6.         f();
  7.         f(i);
  8.         system("pause");
  9.         return 0;
  10. }

  11. int f(int i)
  12. {
  13.         printf("i=%d\n",i);
  14.         return 0;
  15. }
复制代码

记得我以前学这个的时候,是这样理解的:
f(void)指定零参数,f()则对应不确定个数的参数。
如果用函数指针来理解,应该好说一些(当然我在这个程序中都用了不恰当的调用):
  1. #include <stdio.h>;
  2. int f(int);
  3. int f2(int, int);

  4. int main()
  5. {
  6.         int i=10, (*p)();
  7.         p=f;
  8.         p();
  9.         p=f2;
  10.         p(i);
  11.         system("pause");
  12.         return 0;
  13. }

  14. int f(int i)
  15. {
  16.         printf("i=%d\n",i);
  17.         return 0;
  18. }

  19. int f2(int i, int j)
  20. {
  21.         printf("%d\t%d\n", i, j);
  22. }
复制代码

我觉得这个特性只有在用于函数指针的时候才有意义。
平时进行函数声明的时候,无参数的函数应该声明成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.


但是同样在C99 P120中:
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.

我在C99标准中没有找到f()声明有匹配任意参数作用的话。
不知道这个是标准(可能我没找对地方),还只是GCC的扩展?

请大家指教。


关于函数参数(void与空)

不写void是K&R C中的。这个地方可以说是C的一个缺陷。但是现在,在任何一本书中都推荐使用void的写法,《C专家编程》中专门有提到这个。

只是现在各个标准都兼容以前的这种K&R写法。是兼容,已经不推荐使用了。

就好像在K&R C中,函数都是这样定义的:
    1. void fun(i)
    2. int i;
    3. {
    4.    ...
    5. }
    复制代码
现在已经不推荐使用了,但大多数编译器仍能通过而不报任何错误或者警告。

摘自:http://bbs.chinaunix.net/thread-513258-1-1.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值