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); printf("%d\n", i); break; } case 'c': { // A 'char' variable will be promoted to 'int' // A character literal in C is already 'int' by itself int c = va_arg(args, int); printf("%c\n", c); break; } case 'f': { double d = va_arg(args, double); printf("%f\n", d); break; } default: puts("Unknown formatter!"); goto END; } } END: va_end(args); } int main(void) { simple_printf("dcff", 3, 'a', 1.969, 42.5); } |
Output:
3
a
1.969000
42.50000
Variadic arguments - cppreference.com
Variadic functions are functions that may be called with different number of arguments.
Only prototyped function declarations may be variadic. This is indicated by the parameter of the form ... which must appear last in the parameter list and must follow at least one named parameter(until C23). The ellipsis parameter and the proceeding parameter must be delimited by ,.
// Prototyped declaration
int printx(const char* fmt, ...); // function declared this way
printx("hello world"); // may be called with one
printx("a=%d b=%d", a, b); // or more arguments
int printz(...); // OK since C23 and in C++
// Error until C23: ... must follow at least one named parameter
// int printy(..., const char* fmt); // Error: ... must be the last
// int printa(const char* fmt...); // Error in C: ',' is required; OK in C++
At the function call, each argument that is a part of the variable argument list undergoes special implicit conversions known as default argument promotions.
Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the <stdarg.h> library facilities.
Notes
Although old-style (prototype-less) function declarations allow the subsequent function calls to use any number of arguments, they are not allowed to be variadic (as of C89). The definition of such function must specify a fixed number of parameters and cannot use the stdarg.h macros.
// old-style declaration, removed in C23
int printx(); // function declared this way
printx("hello world"); // may be called with one
printx("a=%d b=%d", a, b); // or more arguments
// the behavior of at least one of these calls is undefined, depending on
// the number of parameters the function is defined to take
Example
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
void tlog(const char* fmt,...)
{
char msg[50];
strftime(msg, sizeof msg, "%T", localtime(&(time_t){time(NULL)}));
printf("[%s] ", msg);
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
int main(void)
{
tlog("logging %d %d %d...\n", 1, 2, 3);
}
Output:
10:21:38\] logging 1 2 3...
*** ** * ** ***
[vprintf, vfprintf, vsprintf, vsnprintf, vprintf_s, vfprintf_s, vsprintf_s, vsnprintf_s - cppreference.com](https://en.cppreference.com/w/c/io/vfprintf "vprintf, vfprintf, vsprintf, vsnprintf, vprintf_s, vfprintf_s, vsprintf_s, vsnprintf_s - cppreference.com")
Defined in header \