7.7. Variable Numbers of ArgumentsC allows you to define functions that you can call with a variable number of arguments. These are sometimes called variadic functions. Such functions require a fixed number of mandatory arguments, followed by a variable number of optional arguments. Each such function must have at least one mandatory argument. The types of the optional arguments can also vary. The number of optional arguments is either determined by the values of the mandatory arguments , or by a special value that terminates the list of optional arguments. The best-known examples of variadic functions in C are the standard library functions printf( ) and scanf( ). Each of these two functions has one mandatory argument , the format string. The conversion specifiers in the format string determine the number and the types of the optional arguments. For each mandatory argument, the function head shows an appropriate parameter, as in ordinary function declarations. These are followed in the parameter list by a comma and an ellipsis (...), which stands for the optional arguments. Internally, variadic functions access any optional arguments through an object with the type va_list, which contains the argument information. An object of this typealso called an argument pointercontains at least the position of one argument on the stack. The argument pointer can be advanced from one optional argument to the next, allowing a function to work through the list of optional arguments. The type va_list is defined in the header file stdarg.h. When you write a function with a variable number of arguments, you must define an argument pointer with the type va_list in order to read the optional arguments. In the following description, the va_list object is named argptr. You can manipulate the argument pointer using four macros, which are defined in the header file stdarg.h:
The function in Example 7-9 demonstrates the use of these macros. Example 7-9. Function add( )// The add( ) function computes the sum of the optional arguments. // Arguments: The mandatory first argument indicates the number of // optional arguments. The optional arguments are // of type double. // Return value: The sum, with type double. double add( int n, ... ) { int i = 0; double sum = 0.0; va_list argptr; va_start( argptr, n ); // Initialize argptr; for ( i = 0; i < n; ++i ) // that is, for each optional argument, sum += va_arg( argptr, double ); // read an argument with type double // and accumulate in sum. va_end( argptr ); return sum; } |