C:参数的数目不详 - 无效美孚() [英] C: Unspecified number of parameters - void foo()
问题描述
我读<一个href=\"http://stackoverflow.com/questions/51032/is-there-a-difference-between-foovoid-and-foo-in-c-or-c\">here在 C
无效美孚()
办法函数foo接受的参数数目不详未指定类型
。
谁能给我或点我到一个 C
函数调用参数数目不详的例子吗?可这有什么在 C
被应用到?我无法在网上找到任何东西。
Can anyone give me or point me to an example where a C
function takes an unspecified number of arguments? What can this be applied to in C
? I couldn't find anything on the web.
谢谢!
推荐答案
这是一个的旧式的函数声明。
That's an old-style function declaration.
这声明:
void foo();
声明富
是一个函数返回无效
接受一个不确定的,但固定的数量和类型(S)参数。这并不意味着调用任意参数是有效的;这意味着编译器不能诊断与错号码或参数类型不正确的电话。
declares that foo
is a function returning void
that takes an unspecified but fixed number and type(s) of arguments. It doesn't mean that calls with arbitrary arguments are valid; it means that the compiler can't diagnose incorrect calls with the wrong number or type of arguments.
某处,也许在另一个翻译单元(源文件),必须有一个的定义的功能,也许是:
Somewhere, perhaps in another translation unit (source file), there has to be a definition of the function, perhaps:
void foo(x, y)
long x;
double *y;
{
/* ... */
}
这意味着,富
任何调用的不的传递型长$ C的两个参数$ C>和
双*
是无效的,并已未定义的行为。
This means that any call to foo
that doesn't pass two arguments of type long
and double*
is invalid, and has undefined behavior.
在此之前的1989年的ANSI C标准,这些是唯一的一种函数声明和定义中的语言版本,并编写正确的函数调用的负担完全在程序员。 ANSI C添加的原型的,即指定一个类型函数的参数,它允许编译时的函数调用检查函数声明。 (此功能从早期的C ++借来的。)的现代相当于上面会:
Prior to the 1989 ANSI C standard, these were the only kind of function declaration and definition available in the language, and the burden of writing correct function calls was entirely on the programmer. ANSI C added prototypes, function declarations that specify the types of a function's parameters, which allow compile-time checking of function calls. (This feature was borrowed from early C++.) The modern equivalent of the above would be:
void foo(long x, double *y);
/* ... */
void foo(long x, double *y) {
/* ... */
}
旧样式(非原型)的声明和定义仍然是合法的,但他们是正式的过时的,这意味着,在原则上,他们可以从语言的未来版本中删除 - 尽管因为他们仍然围绕在2011的标准,我不知道这永远不会真正发生
Old-style (non-prototype) declarations and definitions are still legal, but they're officially obsolescent, which means that, in principle, they could be removed from a future version of the language -- though since they're still around in the 2011 standard I don't know that that will ever actually happen.
有没有好的理由使用旧式函数声明和定义在现代的C code。 (我见过的论点在某些角落情况下使用它们,但我觉得他们缺乏说服力。)
There is no good reason to use old-style function declarations and definitions in modern C code. (I've seen arguments for using them in some corner cases, but I find them unconvincing.)
C也支持的可变参数的功能,如的printf
,它需要做的参数任意数量,但是这是一个显着特点。一个可变参数函数必须用原型,其中包括尾随 ...
声明。 (呼叫无可见原型可变参数函数并不违法,但它未定义的行为。)函数本身使用的中所定义的宏; STDARG.H&GT;
来处理其参数。与旧式函数声明,没有编译时检查相应的参数 ...
(尽管一些编译器可以检查一些调用,例如海湾合作委员会警告如果在的printf
调用的参数与格式字符串不一致)。
C also supports variadic functions like printf
, which do take an arbitrary number of arguments, but that's a distinct feature. A variadic function must be declared with a prototype, which includes a trailing , ...
. (Calling a variadic function with no visible prototype isn't illegal, but it has undefined behavior.) The function itself uses macros defined in <stdarg.h>
to process its parameters. As with old-style function declarations, there is no compile-time checking for arguments corresponding to the , ...
(though some compilers may check some calls; for example gcc warns if the arguments in a printf
call are inconsistent with the format string).
这篇关于C:参数的数目不详 - 无效美孚()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!