Variadic functions - cppreference.com
Variadic functions
Variadic functions are functions (e.g. printf) which take a variable number of arguments.
The declaration of a variadic function uses an ellipsis as the last parameter, e.g.
int printf(const char* format, ...);. See variadic arguments for additional detail on the syntax and automatic argument conversions.
Accessing the variadic arguments from the function body uses the following library facilities:
Types
va_list: holds the information needed by va_start, va_arg, va_end, and va_copy (typedef)
Macros
Defined in header <stdarg.h>
va_start:
enables access to variadic function arguments
(function macro)
va_arg:
accesses the next variadic function argument
(function macro)
va_copy:
(C99)
makes a copy of the variadic function arguments
(function macro)
va_end:
ends traversal of the variadic function arguments
(function macro)
Example
Run online compiler: Coliru
Print values of different types.
#include <stdarg.h> #include <stdio.h> void simple_printf(const char* fmt, ...) { va_list args; for (va_start(args, fmt); *fmt != '\0'; ++fmt) { switch(*fmt) { case 'd': { int i = va_arg(args, int); |