Variadic functions
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Variadic functions
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Relational operators (deprecated in C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
Swap and type operations
Common vocabulary types
Variadic functions are functions (e.g. std::printf ) which take a variable number of arguments.
To declare a variadic function, an ellipsis appears after the list of parameters, e.g. int printf(const char* format...);, which may be preceded by an optional comma. See Variadic arguments for additional detail on the syntax, automatic argument conversions and the alternatives.
To access the variadic arguments from the function body, the following library facilities are provided:
Defined in header
<cstdarg>
[edit] Example
Run this code
#include <cstdarg> #include <iostream> void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid { va_list args; va_start (args, fmt); while (*fmt != '0円') { if (*fmt == 'd') { int i = va_arg (args, int); std::cout << i << '\n'; } else if (*fmt == 'c') { // note automatic conversion to integral type int c = va_arg (args, int); std::cout << static_cast<char>(c) << '\n'; } else if (*fmt == 'f') { double d = va_arg (args, double); std::cout << d << '\n'; } ++fmt; } va_end (args); } int main() { simple_printf("dcff", 3, 'a', 1.999, 42.5); }
Output:
3 a 1.999 42.5
[edit] See also
C documentation for Variadic functions