使用数目可变的自变量调用Calls with a Variable Number of Arguments

部分参数列表可由省略号表示法(一个逗号后跟三个句点 (, ...) 终止,以指示可能有多个自变量传递给函数,但没有有关这些自变量的详细信息。A partial parameter list can be terminated by the ellipsis notation, a comma followed by three periods (, ...), to indicate that there may be more arguments passed to the function, but no more information is given about them. 对此类自变量不执行类型检查。Type checking is not performed on such arguments. 省略号表示法前面必须至少有一个参数,并且省略号表示法必须是参数列表中的最后一个标记。At least one parameter must precede the ellipsis notation and the ellipsis notation must be the last token in the parameter list. 如果没有省略号表示法,当函数收到除参数列表中声明的参数以外的参数时,该函数的行为是不确定的。Without the ellipsis notation, the behavior of a function is undefined if it receives parameters in addition to those declared in the parameter list.

若要调用具有可变数量的参数的函数,只需在函数调用中指定任意数量的参数即可。To call a function with a variable number of arguments, simply specify any number of arguments in the function call. 一个示例是 C 运行库中的 printf 函数。An example is the printf function from the C run-time library. 函数调用必须包含参数列表或参数类型列表中声明的每个类型名称的一个参数。The function call must include one argument for each type name declared in the parameter list or the list of argument types.

除非指定 __fastcall 调用约定,否则函数调用中指定的所有参数都将位于堆栈上。All the arguments specified in the function call are placed on the stack unless the __fastcall calling convention is specified. 为函数声明的形参的数量决定了从堆栈获取和分配给形参的实参的数量。The number of parameters declared for the function determines how many of the arguments are taken from the stack and assigned to the parameters. 您负责从堆栈中检索任何其他参数和确定应存在的参数数量。You are responsible for retrieving any additional arguments from the stack and for determining how many arguments are present. STDARG.H 文件包含 ANSI 样式宏,该宏用于访问采用可变数量的参数的函数的参数。The STDARG.H file contains ANSI-style macros for accessing arguments of functions which take a variable number of arguments. 此外,VARARGS.H 中的 XENIX 样式宏仍受支持。Also, the XENIX-style macros in VARARGS.H are still supported.

以下示例声明用于调用可变数量的自变量的函数:This sample declaration is for a function that calls a variable number of arguments:

int average( int first, ...);  

另请参阅See Also

函数调用Function Calls