/* log formatted error messages */ #include < stdio.h> #include < stdarg.h> int eprint (const char *format, ...) { extern FILE *logfile; int n; va_list ap; va_start(ap, format); fprintf(stderr, "\aERROR: "); vfprintf(stderr, format, ap); va_start(ap, format); n = vfprintf(logfile, format, ap); va_end(ap); return (n); } /* first argument is number of numbers to be added; all other arguments are numbers to be added */ int va_sum(int count, ...) { int i, sum = 0; va_list ap; va_start(ap, count); for (i = 0; i < count; i++) { sum += va_arg(ap, int); } va_end(ap); return sum; }
If the above program is compiled and run, we get the output:
sum 1 = 3 sum 2 = 6