The header "stdarg.h" is a standard C header which declares some macros useful for dealing with the functions with varying number of arguments of varying types. The most important macros declared are va_list
, va_start
, va_end
and va_arg
.
The macro va_list
represents an abstract datatype that can store the "unnamed" arguments of the function call (those that are captured by the "..."). To use it, you must first initialize it, using va_start()
macro:
va_list argptr; /* declaration of the unnamed arguments list */
va_start(argptr, num); /* the initialization of the list */
The second parametar of va_start()
must be the last named parameter of the function (in your case, this is num
, which is the only named parameter provided). After the initialization, you can extract the unnamed arguments from the argptr
list one by one, using va_arg()
macro:
answer += va_arg(argptr, int);
Each call of va_arg()
returns the next argument from argptr
, interpreting it as int
(or any other type that is provided as the second argument of va_arg()
). Notice that there is no type-checking and there is no way to be sure that the caller has actually provided the arguments of correct types.
Finally, the list should be released at the end by calling va_end(argptr)
before returning.