#include <stdio.h>
#include <stdarg.h>// n 元素个数, ...可变参数
// 返回累加和
double sum(int n, ...)
{va_list ap;int i = 0;double d = 0;va_start(ap, n);for (i = 0; i < n; i++){d += va_arg(ap, double); // 每次提取一个}va_end(ap);return d;
}int main(void)
{double d_sum = 0;d_sum = sum(3, 1.2, 2.3, 1.5);printf("sum=%.1llf\n", d_sum);d_sum = sum(4, 1.2, 2.3, 1.5, 2.5);printf("sum=%.1llf\n", d_sum);return 0;
}