参考: http://www.tutorialspoint.com/c_standard_library/c_macro_va_start.htm (这的代码可以执行的哦)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdarg.h> #include<stdio.h> int sum(int, ...); int main(void) { printf("Sum of 10, 20 and 30 = %d\n", sum(3, 10, 20, 30) ); printf("Sum of 4, 20, 25 and 30 = %d\n", sum(4, 4, 20, 25, 30) ); return 0; } int sum(int num_args, ...) { int val = 0; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { val += va_arg(ap, int); } va_end(ap); return val; } |