// clang variadic.c -o prog
#include <stdio.h>
#include <stdarg.h>
int f(int count, ...)
{
printf("%s%s", __func__, "\n");
int n, sum;
va_list ap;
va_start(ap, count);
sum = 0;
for ( ; count > 0; count--) {
n = va_arg(ap, int);
printf("n = %3d\n", n);
sum += n;
}
va_end(ap);
return sum;
}
int g(int x, ...)
{
printf("%s%s", __func__, "\n");
fflush(stdout);
int n, sum;
va_list ap;
va_start(ap, x);
n = x;
sum = 0;
while (!(0)) {
printf("n = %3d\n", n);
sum += n;
n = va_arg(ap, int);
if (n == 0) { break; }
}
va_end(ap);
return sum;
}
int main(int argc, char * argv[]) {
int result = f(3,1,2,3);
printf ("sum = %d\n", result);
result = g(1,2,3,0);
printf ("sum = %d\n", result);
return 0;
} |