Difference between revisions of "c/variadic"
From cppreference.com
< c
m (References; language links; title of link now matches title of targeted page) |
m (→See also: nomono, langlinks) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 27: | Line 27: | ||
#include <stdarg.h> | #include <stdarg.h> | ||
− | void simple_printf(const char* fmt,...) | + | void simple_printf(const char* fmt, ...) |
{ | { | ||
va_list args; | va_list args; | ||
Line 37: | Line 37: | ||
printf("%d\n", i); | printf("%d\n", i); | ||
} else if (*fmt == 'c') { | } else if (*fmt == '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); | int c = va_arg(args, int); | ||
printf("%c\n", c); | printf("%c\n", c); | ||
Line 67: | Line 68: | ||
{{ref std | section=7.15 | title=Variable arguments <stdarg.h> | p=249-252}} | {{ref std | section=7.15 | title=Variable arguments <stdarg.h> | p=249-252}} | ||
{{ref std c89}} | {{ref std c89}} | ||
− | {{ref std | section= | title=}} | + | {{ref std | section=4.8 | title=VARIABLE ARGUMENTS <stdarg.h>}} |
{{ref std end}} | {{ref std end}} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc see cpp | cpp/utility/variadic | Variadic functions}} | + | {{dsc see cpp | cpp/utility/variadic | Variadic functions | nomono=true}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|ar|cs|de|es|fr|it|ja|ko|pl|pt|ru|tr|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 03:58, 23 June 2021
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:
Macros | ||
Defined in header
<stdarg.h> | ||
enables access to variadic function arguments (function macro) | ||
accesses the next variadic function argument (function macro) | ||
(C99) |
makes a copy of the variadic function arguments (function macro) | |
ends traversal of the variadic function arguments (function macro) | ||
Type | ||
holds the information needed by va_start, va_arg, va_end, and va_copy (typedef) |
[edit] Example
Print values of different types.
Run this code
#include <stdio.h> #include <stdarg.h> 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); printf("%d\n", i); } else if (*fmt == '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); } else if (*fmt == 'f') { double d = va_arg(args, double); printf("%f\n", d); } ++fmt; } va_end(args); } int main(void) { simple_printf("dcff", 3, 'a', 1.999, 42.5); }
Output:
3 a 1.999000 42.50000
[edit] References
- C11 standard (ISO/IEC 9899:2011):
- 7.16 Variable arguments <stdarg.h> (p: 269-272)
- C99 standard (ISO/IEC 9899:1999):
- 7.15 Variable arguments <stdarg.h> (p: 249-252)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.8 VARIABLE ARGUMENTS <stdarg.h>
[edit] See also
C++ documentation for Variadic functions
|