《C标准5.2.4.1》规定:
4095 characters in a logical source line
127 parameters in one function definition
127 arguments in one function call
同时,如果您的参数是巨大的结构体,则堆栈大小(1MB - 8MB)是一个限制。
但是,所有这些限制都远离良好的实践。
https://gcc.gnu.org/onlinedocs/gcc-4.3.5/cpp/Implementation-limits.html 表示gcc有更高的限制(仅受可用内存限制)。
gcc
文档仅涵盖C预处理器,虽然有一定指示作用,但并未真正涵盖此情况。 - Shafik Yaghmourint a_function ( int x, ... )
{
va_list a_list;
va_start( a_list, x );
}
我认为没有特定的限制。
以下是如何使用该库的示例: (来源:http://www.cprogramming.com/tutorial/c/lesson17.html)
#include <stdarg.h>
#include <stdio.h>
/* this function will take the number of values to average
followed by all of the numbers to average */
double average ( int num, ... )
{
va_list arguments;
double sum = 0;
/* Initializing arguments to store all values after num */
va_start ( arguments, num );
/* Sum all the inputs; we still rely on the function caller to tell us how
* many there are */
for ( int x = 0; x < num; x++ )
{
sum += va_arg ( arguments, double );
}
va_end ( arguments ); // Cleans up the list
return sum / num;
}
int main()
{
/* this computes the average of 13.2, 22.3 and 4.5 (3 indicates the number of values to average) */
printf( "%f\n", average ( 3, 12.2, 22.3, 4.5 ) );
/* here it computes the average of the 5 values 3.3, 2.2, 1.1, 5.5 and 3.3
printf( "%f\n", average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) );
}
标准不知道,但在堆栈上,您可以传递任意数量的参数,如果通过寄存器传递,则数量受寄存器数量的限制。在C语言中,您可以通过堆栈按值传递结构。