C 函数声明中的无类型参数 [英] Untyped arguments in a C function declaration
问题描述
最近我一直在看 Steven Skiena 的在线资源中的一些 C 示例代码 《算法设计手册》 并被他的一些函数调用的语法所迷惑.诚然,自从在 uni 上使用 C 已经有一段时间了,但我从未遇到过这样的无类型函数参数:
Recently I've been looking at the some of the C example code from the online resources of Steven Skiena's "Algorithm Design Manual" and have been baffled by the syntax of some of his function calls. Admittedly it's been a while since did C at uni but I've never encountered untyped function arguments like this:
find_path(start,end,parents)
int start;
int end;
int parents[];
{
if ((start == end) || (end == -1))
printf("\n%d",start);
else {
find_path(starts,parents[end],parents);
printf(" %d",end);
}
}
这是有效的语法吗?这种风格的函数声明是否有任何好处?它似乎比传统的内联参数类型更冗长.
Is this valid syntax anymore? Are / were there any benefits with this style of function declaration? It seems more verbose than the conventional inline typing of arguments.
推荐答案
它们被称为 K&R 样式定义.不要在新代码中使用它们.甚至 K 和 R 也建议你在The C Programming Language 2ed"中远离它们.
They are called K&R style definitions. Don't use them in new code. Even K and R recommend that you stay away from them in "The C Programming Language 2ed".
历史记录:ANSI C 与更早版本之间的最大变化版本是函数的声明和定义方式.
A note of history: the biggest change between ANSI C and earlier versions is how functions are declared and defined.
参数在括号之间命名,它们的类型是在打开左大括号之前声明;未声明的参数是取为int
.
The parameters are named between the parentheses, and their types are
declared before opening the left brace; undeclared parameters are
taken as int
.
函数原型的新语法使编译器来检测参数数量或其类型的错误.旧风格的声明和定义仍然适用于 ANSI C,在至少在过渡期,但我们强烈建议您使用当你有一个支持它的编译器时的新形式.
The new syntax of function prototypes makes it much easier for a compiler to detect errors in the number of arguments or their types. The old style of declaration and definition still works in ANSI C, at least for a transition period, but we strongly recommend that you use the new form when you have a compiler that supports it.
这篇关于C 函数声明中的无类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!