Variadic Functions
A variadic function accepts a fixed list of named parameters followed by an unknown number of extra arguments, written as .... The callee walks them with the macros in <stdarg.h>.
Detailed Explanation
printf is the existence proof. Its first parameter, the format string, tells the function how many extra arguments there are and what types they have. The compiler does not pass a hidden argc. If the format and the arguments disagree, the function reads the stack (or the register save area) as the wrong type. That is undefined behavior.
You write a variadic function like this. At least one named parameter is required; it is the anchor. In the body you declare a va_list, call va_start(ap, last_named), pull values with va_arg(ap, Type), and finish with va_end(ap). To walk the list twice, va_copy the list first. Forgetting va_end leaks implementation resources on some ABIs.
va_arg needs the exact type after promotion. You cannot ask for char or float; those promote to int and double before the call. Ask for int and double. Asking for the wrong type — int when the caller passed a pointer, float when the caller passed a double — is undefined behavior and will not be diagnosed unless the call site is a format-checked builtin.
That is the central cost. Variadic functions disable type checking for the trailing arguments. They also disable the ability to pass a va_list through a wrapper unless you provide a v-prefixed sibling: vprintf, vsnprintf. If you wrap printf, write vfprintf into the wrapper and take a va_list, or you will not be able to forward.
Reach for ... when you are writing a printf-like logger, or a function that really does take a heterogeneous list that a format or a sentinel can describe. A sentinel-terminated list of pointers (execl-style) is the other classic pattern; the caller must pass (char *)NULL as the last argument, and a missing sentinel is again undefined. For a homogeneous list of ints, pass a pointer and a count instead.
Code Examples
#include <stdarg.h>
#include <stdio.h>
static int sum_ints(int count, ...)
{
va_list ap;
va_start(ap, count);
int total = 0;
for (int i = 0; i < count; i++) {
total += va_arg(ap, int); /* promoted type: int, not short or char */
}
va_end(ap);
return total;
}
int main(void)
{
printf("%d\n", sum_ints(3, 10, 20, 12)); /* prints: 42 */
return 0;
}#include <stdarg.h>
#include <stdio.h>
static void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap); /* forward; do not call printf here */
va_end(ap);
fputc('\n', stderr);
}
int main(void)
{
die("missing %s: %d", "id", 7); /* prints to stderr: missing id: 7 */
return 0;
}Common Gotchas
The caller and `va_arg` must agree on the promoted type
`va_arg(ap, float)` is always wrong; the caller passed a `double`.
A wrapper that calls `printf` instead of `vprintf` cannot forward `...`
you need a `va_list` and the `v*` function.
There is no portable way to ask "how many arguments remain"
a count, a format, or a sentinel is required, and a wrong one is undefined behavior.
Default argument promotions apply
`char`, `short`, and `_Bool` arrive as `int`. `float` arrives as `double`.