/* An example for variable number of arguments. The six places where variable arguments occur are flagged with a "VA" Determine the minimum of a number of doubles. There are two strategies for handling arguments: 1) Use an argument count to detect the end of arguments. printf() uses a this strategy: the format string tells printf what to expect. vprintf() and friends can handle va_list's. 2) Use a sentinel to signal the end of arguments, for example, a NULL pointer to signal the end of pointer arguments. */ #include /* VA */ /* defines the required macros */ #include double min_dble(int argcnt, ... /* VA */ ) { va_list ap; /* VA */ /* argument pointer */ double min = DBL_MAX; /* Make ap point to argument after "argcnt" */ va_start(ap,argcnt); /* VA */ while(argcnt-- > 0) { double next; /* pick off next double argument and advance argument pointer */ next = va_arg(ap, double); /* VA */ if(next < min) min = next; } va_end(ap); /* VA */ /* clean up - free alloced space if any */ return min; } int main() { printf("Minimum of %lf, %lf, %lf is %lf\n",2.0,-3.0,6.0, min_dble(3, 2.0,-3.0,6.0)); printf("Minimum of %lf, %lf is %lf\n",-8.1,-9.2, min_dble(2, -8.1,-9.2)); return 0; }