在C中使用需要任意数量参数的函数的参数 [英] Use the parameters of a function that takes any number of parameters, in C
问题描述
我刚刚阅读:"C无效参数" 关于这些函数定义之间的区别C:
I've just read: "C void arguments" about the differences between these function definitions in C:
int f(void)
和
int f()
理解第二种形式意味着一个返回带有任意数量参数的整数的函数,我想知道我们如何才能实际访问和使用这些未知参数?
Understanding that the second form means a function returning integer that takes any number of parameters, I wondered how can we actually access and use these unknown parameters?
我很想获得示例代码和说明.
I'd love to get sample code and explanation.
此外,我知道C语言中Varargs的机制(具有va_arg
,va_end
,va_start
函数),并且很高兴听到该机制与f()
形式之间的区别.
Also, I know the mechanism of the Varargs in C (with va_arg
, va_end
, va_start
functions) and would be glad to hear about the differences between this mechanism and the f()
form mentioned above.
非常感谢!
推荐答案
int f(void)
表示返回不带参数的int的函数.
int f(void)
means a function returning int that takes no parameters.
您可以看到此示例,以了解使用int f()的结果
You can see this example to see the consequence of using int f()
#include <stdio.h>
void f();
int main() {
f(); /* prints some garbage */
f(16); /* prints 16 */
return 0;
}
void f(a1)
int a1;
{
printf("%d\n", a1);
}
还有这个:-
#include <stdio.h>
#include <stdlib.h>
int f();
int f(int x) {
return x;
}
int main (int argc, char *argv[]) {
printf ("%d\n", f(atoi(argv[1])));
return 0;
}
pax> gcc -Wall --std=c99 -o qq qq.c
pax> ./qq 42
42
这篇关于在C中使用需要任意数量参数的函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!