Variable Number Of Arguments - cont.
#include <stdarg.h> int sum(int argcnt, ….) /*argcnt is num of args */{ va_list ap; /* argument pointer (macro) */ int ans = 0; va_start(ap,argcnt); /* init ap */ while (argcnt-- > 0) /* process all args */ ans += va_arg(ap,int); /*va_arg advances ap */ va_end(ap); /* clean up */ return(ans);}
Use: int total = sum(5, 85, 90, 97, 79, 96);