Variadic functions
The printf() function allows multiple arguments. It is a variadic function. If you want to write variadic functions, you use ellipsis ( ... ) as a parameter.int sumupparameters(int value,...) ;
Getting the values from variadic Parameters
From the standard header files (see the first Page of this lesson), we use <stdarg.h>. This provides macros that let you access the parameters.Macros There are three macros defined in <stdarg.h> and one type va_list.
- va_start( va_list ap, lastarg) ; // Start with first parameter after lastarg
- va_arg(va_list ap,type) ; // fetch value of type 'type'
- va_end(va_list ap) ; // must do to complete
#include <stdarg.h>On the next page : Learn how this code works.
#include <stdio.h>
#include "stdafx.h"
double total ( int numvalues, ... )
{
va_list values;
double temptotal = 0;
va_start ( values, numvalues ) ;
while ( numvalues-- )
{
temptotal += va_arg ( values, double ) ;
}
va_end ( values ) ;
return temptotal;
}
int main()
{
printf( "%f\n", total ( 4, 1.0, 5.9, 107.3, 95.8 ) ) ;
return 0;
}

