From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Fvariadic.html | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 devdocs/c/language%2Fvariadic.html (limited to 'devdocs/c/language%2Fvariadic.html') diff --git a/devdocs/c/language%2Fvariadic.html b/devdocs/c/language%2Fvariadic.html new file mode 100644 index 00000000..610036e5 --- /dev/null +++ b/devdocs/c/language%2Fvariadic.html @@ -0,0 +1,73 @@ +

Variadic arguments

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:

+ + + + + + +
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)
holds the information needed by va_start, va_arg, va_end, and va_copy
(typedef)

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...

References

See also

+
C++ documentation for Variadic arguments
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/variadic +

+
-- cgit v1.2.3