Variadic arguments
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 19 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Only prototyped MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 17 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE 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 哋它亢23). 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 哋它亢23 and in 哋它亢++ // Error until 哋它亢23: ... 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 哋它亢++
At the MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 16 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, each argument that is a part of the variable argument list undergoes special implicit conversions known as MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 15 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
Within the body of a function that uses variadic arguments, the values of these arguments may be accessed using the <stdarg.h>
library facilities:
Defined in header
<stdarg.h> | |
enables access to variadic function arguments (function macro) | |
accesses the next variadic function argument (function macro) | |
(哋它亢99) |
makes a copy of the variadic function arguments (function macro) |
ends traversal of the variadic function arguments (function macro) | |
holds the information needed by va_start, va_arg, va_end, and va_copy (typedef) |
Notes
Although old-style (prototype-less) MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 14 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE allow the subsequent function calls to use any number of arguments, they are not allowed to be variadic (as of 哋它亢89). The definition of such function must specify a fixed number of parameters and cannot use the stdarg.h
macros.
// old-style declaration, removed in 哋它亢23 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]; MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 12 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(msg, sizeof msg, "%T", MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 11 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(&(MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 10 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE){MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 09 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 07 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE)})); MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 06 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE("[%s] ", msg); va_list args; va_start(args, fmt); MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 05 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE(fmt, args); va_end(args); } int main(void) { tlog("logging %d %d %d...\n", 1, 2, 3); }
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 10 HOURS 51 MINUTES 04 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
[10:21:38] logging 1 2 3...
References
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.7.6.3/9 Function declarators (including prototypes) (p: 96)
- 7.16 Variable arguments <stdarg.h> (p: 197-199)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.7.6.3/9 Function declarators (including prototypes) (p: 133)
- 7.16 Variable arguments <stdarg.h> (p: 269-272)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.7.5.3/9 Function declarators (including prototypes) (p: 119)
- 7.15 Variable arguments <stdarg.h> (p: 249-252)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 3.5.4.3/5 Function declarators (including prototypes)
- 4.8 VARIABLE ARGUMENTS <stdarg.h>