- 论坛徽章:
- 0
|
刚才在看C与C++之争的那个帖子,有一位朋友指出一个问题,认为这是C不严谨的地方
- #include <stdio.h>;
- int f();
- int main()
- {
- int i=10;
- f();
- f(i);
- system("pause");
- return 0;
- }
- int f(int i)
- {
- printf("i=%d\n",i);
- return 0;
- }
复制代码
记得我以前学这个的时候,是这样理解的:
f(void)指定零参数,f()则对应不确定个数的参数。
如果用函数指针来理解,应该好说一些(当然我在这个程序中都用了不恰当的调用):
- #include <stdio.h>;
- int f(int);
- int f2(int, int);
- int main()
- {
- int i=10, (*p)();
- p=f;
- p();
- p=f2;
- p(i);
- system("pause");
- return 0;
- }
- int f(int i)
- {
- printf("i=%d\n",i);
- return 0;
- }
- int f2(int i, int j)
- {
- printf("%d\t%d\n", i, j);
- }
复制代码
我觉得这个特性只有在用于函数指针的时候才有意义。
平时进行函数声明的时候,无参数的函数应该声明成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的扩展?
请大家指教。 |
|