函数语法,参数列表后声明的参数类型我对C比较陌生。我遇到了一种以前从未见过的函数语法,在参数列表之后定义了参数类型。有人能向我解释一下它与典型的C函数语法有何不同吗?例子:int main (argc, argv)int argc;char *argv[];{return(0);}
3 回答
潇湘沐
TA贡献1816条经验 获得超6个赞
这是参数列表的旧式语法,它仍然被支持。在K&R C中,您还可以关闭类型声明,它们将默认为int。E.
main(argc, argv)char *argv[];{ return 0;}
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
void f(a) float a; { /* ... */}
f
double
float
void f(float a);void f(a) float a; {}
double
float
// option 1void f(double a);void f(a) float a; {}// option 2// this declaration can be put in a header, but is redundant in this case, // since the definition exposes a prototype already if both appear in a // translation unit prior to the call. void f(float a); void f(float a) {}
婷婷同学_
TA贡献1844条经验 获得超8个赞
#include <stdio.h>int foo(c)int c;{ return printf("%d\n", c); }int bar(x)double x;{ return printf("%f\n", x); }int main(void){ foo(42); /* ok */ bar(42); /* oops ... 42 here is an `int`, but `bar()` "expects" a `double` */ return 0;}
$ gcc proto.c $ gcc -Wstrict-prototypes proto.c proto.c:4: warning: function declaration isn’t a prototype proto.c:10: warning: function declaration isn’t a prototype $ ./a.out 42 0.000000
- 3 回答
- 0 关注
- 487 浏览
添加回答
举报
0/150
提交
取消