C examples for Function:Variable Length Argument
In the following function accepts a variable number of arguments that are summed up and returned to the caller.
#include <stdio.h> #include <stdarg.h> int sum(int num, ...) { va_list args; /* variable argument list */ int sum = 0, i = 0; va_start(args, num); /* initialize argument list */ for (i = 0; i < num; i++) /* loop through arguments */ sum += va_arg(args, int); /* get next argument */ va_end(args); /* free memory */ return sum;/* w w w .j a v a 2s . com*/ } int main(void) { printf("Sum of 1+2+3 = %d", sum(3,1,2,3)); /* 6 */ }