简体   繁体   English

未指定的参数个数和传递给C函数的变量的个数之间有什么区别?

[英]What is difference between unspecified no of arguments and variable no of arguments passed to a C function?

#include <stdio.h>
#define L 11

int oo1(){
printf("oooooo\n");
}

int oo2(int C ,...)
{
 //some code here
 printf("in oo2\n");
}
int main(void){
 oo1();
 oo1(1,4,7,"f");
 oo2(2);
 oo2(2,5,7,3,11);
 printf("%d\n",L);
}

OUTPUT : 输出:

oooooo

oooooo

in oo2

in oo2

11

This code has 2 functions oo1 and oo2 . 该代码具有2个函数oo1和oo2。 oo2 is a function that accepts variable number of arguments . oo2是一个接受可变数量参数的函数。 However , oo1 accepts variable or any number of arguments too as it seems . 但是,oo1似乎也接受变量或任意数量的参数。 What is the difference between these two and how do they work ? 两者之间有什么区别,它们如何工作? Any link or reference would be helpful too if this seems too obvious . 如果这看起来太明显,则任何链接或参考也将有所帮助。 Thanks . 谢谢 。

The difference is that it's unspecified what happens for oo1 . 区别在于,对于oo1会发生什么oo1

oo1 accepts variable or any number of arguments too as it seems oo1似乎也接受变量或任意数量的参数

The C standard does not guarantee this. C标准不保证这一点。 In fact, all the C standard says on this regard is that a function definition with an empty argument list is compatible with a function declaration with an argument list of void (ISO/IEC 9899 6.9.1, footnote 162). 事实上,所有的C标准说在这方面是有一个空的参数列表的功能定义是用的参数列表一个函数声明兼容void (ISO / IEC 9899 6.9.1,脚注162)。

That is, the function definition 也就是说,函数定义

int oo1() {…}

Is compatible with the prototype declaration 与原型声明兼容

int oo1(void);

In other words, the only safe thing you may do is call oo1 without arguments. 换句话说,唯一安全的操作是不带参数的oo1调用。 But the C compiler is not obliged to diagnose violations of this. 但是C编译器没有义务诊断是否违反此规则。 That said, a modern C compiler will warn: 也就是说,现代的C编译器警告:

warning: too many arguments in call to 'oo1' 警告:调用“ oo1”的参数过多

Heed this warning. 注意此警告。 You mustn't call oo1 with any arguments. 您不得使用任何参数调用oo1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM