C examples for Function:Variable Length Argument
Use variable number of arguments
#include <stdio.h> #include <stdarg.h> double sum(int, ...); int main(void) { double s,t;/*from w w w. j a v a2 s . c o m*/ s = sum(13, 1.1, 12.15, 13.13); t = sum(16, 11.11, 2.11, 113.1, 14.1, 51.1, 16.1); printf("%g\n", s); printf("%g\n", t); return 0; } double sum(int lim,...){ va_list ap; // declare object to hold arguments double tot = 0; int i; va_start(ap, lim); // initialize ap to argument list for (i = 0; i < lim; i++) tot += va_arg(ap, double); // access each item in argument list va_end(ap); // clean up return tot; }