#include #include /* A demonstration of the variable number of arguments * feature in C. Here, 'average' takes an arbitrary * (but known) number of arguments and averages them. * * The idea is from wikipedia: * en.wikipedia.org/wiki/Varargs */ double average( int count, ... ); int main( void ) { printf( "The average of the digits is %lf\n", average( 4, 3.52, 4.72, 4.19, 3.94 ) ); printf( "The average of the digits is %lf\n", (3.52 + 4.72 + 4.19 + 3.94)/4.0 ); return 0; } double average( int count, ... ) { va_list ap; int i; double sum = 0.0; va_start( ap, count ); for ( i = 0; i < count; i++ ) { sum += va_arg( ap, double ); } va_end( ap ); return sum/count; }