c 可变参数demo
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> int average(int num, ...); int main() { int averageRes = average(3, 1, 3, 5); printf("average:%d\n", averageRes); return 0; } int average(int num, ...) { va_list list; int sum = 0; va_start(list, num); for (int i = 0; i < num; i++) { sum += va_arg(list, int); } return sum / num; }
Please call me JiangYouDang!