Which header defines va_list, va_start, va_arg and va_end?
<stdarg.h> is the standard header. <varargs.h> was a pre-standard Unix header and is obsolete.
Variable Number of Arguments · Exercise 1 of 4
Twelve multiple-choice questions on variadic functions, stdarg.h macros, promotions, wrappers, variadic macros and C23. Pick an option to check your answer instantly.
Which header defines va_list, va_start, va_arg and va_end?
<stdarg.h> is the standard header. <varargs.h> was a pre-standard Unix header and is obsolete.
Where can ... appear in a function's parameter list?
Named parameters come first, then the ellipsis. int f(..., int n) is a syntax error.
In int f(int a, double b, ...), how should va_start be called (C17)?
va_start needs the last named parameter so it knows where the variable part begins. In C23 the second argument is optional.
How does a variadic function find out how many arguments were passed?
Nothing marks the end of the arguments. Reading past them is undefined behaviour, so the function relies on a convention.
A caller passes a float variable to a variadic function. How must it be read?
The default argument promotions turn float into double when it matches .... Reading it as float is undefined behaviour.
How should a char argument be read with va_arg?
char (and short and bool) are promoted to int, so read an int and convert it back if needed.
How does printf() decide how many arguments to read?
Each %-conversion consumes one argument (two for %*d). Too few arguments for the format is undefined behaviour.
Why would a function use va_copy()?
After va_arg has walked a va_list (or it was passed to vprintf), its state is consumed. A copy made earlier can walk the arguments again.
Why does the library provide vprintf(), which takes a va_list?
A logger calls va_start and hands the va_list to vprintf or vfprintf. Every printf-family function has such a v-version.
A function calls va_start but returns without calling va_end. What does the standard say?
On common platforms va_end happens to do nothing, but some implementations allocate in va_start. Always call it.
Timed online tests with instant scores and explanations.