Namespaces
Variants

Vararg functions utilities

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Undefined behavior utilities
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
 
 

Provides utilities to access the extra arguments accepted by vararg functions.


Defined in header <cstdarg>
enables access to the extra arguments accepted by vararg functions
(function macro) [edit]
accesses the next extra argument
(function macro) [edit]
(C++11)
makes a copy of the extra arguments
(function macro) [edit]
ends traversal of the extra arguments
(function macro) [edit]
holds the information needed by va_start, va_arg, va_end, and va_copy
(typedef) [edit]

Example

#include <cstdarg>
#include <iostream>

void simple_printf(const char* fmt, ...)
{
    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

See also

C documentation for Vararg functions