c语言重载(overriding in C)或函数不定参数个数
2015-04-15 10:35
253 查看
google一下 c overiding发现有这样一段英文解释:
Because C doesn't require that you pass all parameters to the function if you leave the parameter list blank in the prototype. The compiler should only throw up warnings if the prototype has a non-empty parameter list and you don't pass enough enough arguments to the function.
在c语言里面如果函数原型参数列表为空,编译器不会要求你把所有参数传递给函数。
如果编译器发现函数原型参数列表非空,并且没有传递足够的参数给函数,他应该仅仅只抛出一个警告。
通常函数fcnt在头文体以int fcntl (int fd, int cmd, ...)方式定义,意味着他可以接受不定个数的参数,
你可以在linux下通过man va_arg等等查看详情。
Because C doesn't require that you pass all parameters to the function if you leave the parameter list blank in the prototype. The compiler should only throw up warnings if the prototype has a non-empty parameter list and you don't pass enough enough arguments to the function.
在c语言里面如果函数原型参数列表为空,编译器不会要求你把所有参数传递给函数。
如果编译器发现函数原型参数列表非空,并且没有传递足够的参数给函数,他应该仅仅只抛出一个警告。
itsme@dreams:~/C$ cat param.c #include <stdio.h> void func(); int main(void) { func(1); func(17, 5); return 0; } void func(int a, int b) { if(a == 1) puts("Only going to use first argument."); else printf("Using both arguments: a = %d, b = %d\n", a, b); } itsme@dreams:~/C$ gcc -Wall -ansi -pedantic param.c -o param itsme@dreams:~/C$ ./param Only going to use first argument. Using both arguments: a = 17, b =5
actually fcnt is defined in the header file using int fcntl (int fd, int cmd, ...); This means it can take variable number of arguments. now it looks at the command and then decides if anything follows as the third parameter or not. see man va_arg etc for more details.
通常函数fcnt在头文体以int fcntl (int fd, int cmd, ...)方式定义,意味着他可以接受不定个数的参数,
你可以在linux下通过man va_arg等等查看详情。
#include <stdio.h> #include <stdarg.h> void printargs(int args1,...)//输出所有的int类型的参数,直到-1结束 { va_list ap; int i; va_start(ap,args1); for (i = args1; i != -1; i =va_arg(ap,int)) printf("%d ",i); va_end(ap); putchar('\n'); } int main(void) { printargs(5,2,14,84,97,15,24,48,-1); printargs(84,51,-1); printargs(-1); printargs(1,-1); return 0; }
相关文章推荐
- 如何写参数个数不定的函数(即类似printf的函数)
- C语言不定参数函数编写
- 不定参数函数调用不定参数函数
- Delphi中调用C约定不定参数函数的调用方法.
- C++ 函数中如何接收数量不定的函数参数
- Python给函数传递不定个数的参数
- Java函数的不定参数
- c/c++函数不定参数定义
- C 的不定参数函数转发,如printf
- Class<T> 和 函数使用不定数目的参数
- Python给函数传递不定关键字的参数
- Java函数的不定参数
- va_list实现不定参数C函数
- va_arg() 实现不定参数函数 返回参数不匹配问题
- 传不定个数参数的函数写法
- 不定数量参数的函数
- c++中参数不定函数和va_list的使用
- 不定参数函数
- 一个DELPHI程序要调用DLL,而DLL中的函数名,参数个数,参数类型为不定
- Java函数的不定参数