在C函数声明无类型参数 [英] Untyped arguments in a C function declaration
问题描述
最近我一直在看一些从史蒂芬Skiena的的算法设计手册,并已经被他的一些函数调用的语法百思不得其解。诚然,它已经有一段时间,因为在大学做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样式定义。不要在新的code使用它们。即使K和ř建议你离他们远在C编程语言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屋!