#include <stdio.h>
#include <stdarg.h>
int average(int n, ...)
{
va_list arg;
int i = 0;
int sum = 0;
va_start(arg, n);
for (i = 0; i<n; i++)
{
sum += va_arg(arg, int);
}
return sum / n;
va_end(arg);
}
int main()
{
int a = 10;
int b = 2;
int c = 8;
int avg1 = average(2, a, c);
int avg2 = average(3, a, b, c);
printf("avg1=%d\n", avg1);
printf("avg2=%d\n", avg2);
system("pause");
return 0;
}
运行结果如下: