Define the variable parameters, use macros are:
va_start (ap, arg) to initialize a variable of the va_list ap
va_arg (ap, type) Get the next type of parameter type
va_end (ap) finished using ap
#include <stdarg.h> #include <stdio.h> int sum(int cnt, ...) { int sum = 0; va_list ap; va_start(ap, cnt); for (int i = 0; i < cnt; ++i) { sum += va_arg(ap, int); } va_end(ap); return sum; } int main() { int result = sum(3, 1, 2, 3); printf("the result is %d\n", result); return 0; }
It is output: the result is 6