<stdarg.h> lets you write functions with a variable number of arguments using ... (ellipsis)—the same idea behind printf. You walk the list with va_list, va_start, va_arg, and va_end.
Remember
#include <stdarg.h>
int f(int count, ...) { // fixed args, then ...
va_list ap;
va_start(ap, count); // last fixed parameter
x = va_arg(ap, int); // next arg (you choose the type)
va_end(ap);
}
Need a way to know how many / which types:
count · format string · NULL sentinel
[INFO] User Alice logged in (id=42)
[WARN] Retries left: 3
How It Works
Print the level. A normal printf writes [INFO] or [WARN].
Open the list.va_start(args, fmt) anchors on the last fixed parameter (fmt).
Forward to vprintf. The format string tells vprintf which types to read from args.
Close the list.va_end(args) cleans up after the formatted message.
Edge Cases & Pitfalls
Check these before shipping variadic APIs.
types
Wrong va_arg type is undefined
Reading an int as a double (or the reverse) is undefined behavior. Match the caller’s actual types.
promote
Respect default promotions
Use va_arg(ap, double) for floating values and va_arg(ap, int) for small integers—not float or char.
count
Never over-read the list
Calling va_arg more times than there are arguments is undefined. Trust a count, format, or sentinel.
cleanup
Always call va_end
Pair every va_start with va_end. To walk the list twice, use C99 va_copy.
Analysis
Performance Notes
Approach
Cost
Best for
va_arg loop
Cheap per argument
Small N (logs, sums)
Array / pointer + length
Often clearer / inlinable
Hot numeric code
vprintf wrappers
Dominated by formatting I/O
Debug / logging APIs
Variadic macros are rarely a bottleneck. Prefer fixed parameters or arrays when types matter more than flexible arity.
Remember
Key Takeaways
Role: read ... args with va_list macros.
Lifecycle:va_start → va_arg → va_end.
Convention: count, format, or sentinel—you choose.
Types: match callers; respect promotions.
One line:stdarg.h walks the ... list—but only you know how many args and which types are there.
Frequently Asked Questions
stdarg.h provides macros to read variable arguments in functions declared with ... (ellipsis). va_start initializes a va_list, va_arg fetches each argument by type, and va_end cleans up. printf and scanf use this machinery internally.
va_start(ap, last) needs the name of the last fixed parameter before the ... arguments. The implementation uses it to find where the variable args begin. It must be the parameter immediately before ...
printf reads the format string (%d, %f, %s) to decide which type to pass to va_arg each time. Your functions must use a similar convention—pass a count, a format string, or a sentinel—because the compiler does not type-check ... arguments.
In variable argument lists, char and short are promoted to int, and float is promoted to double. Always use va_arg(ap, int) for promoted char/short and va_arg(ap, double) for float values passed through ...
va_list is a type (typedef), not a macro. va_start, va_arg, va_end, and va_copy (C99) are macros. You declare va_list ap; then call va_start(ap, last_fixed_arg).
Use them for logging, printf-style APIs, or wrappers where arity varies. For most application code, fixed parameters, arrays, or structs are safer. Prefer stdarg when you truly need flexible arity.