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/variadic%2Fva_arg.html | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 devdocs/c/variadic%2Fva_arg.html (limited to 'devdocs/c/variadic%2Fva_arg.html') diff --git a/devdocs/c/variadic%2Fva_arg.html b/devdocs/c/variadic%2Fva_arg.html new file mode 100644 index 00000000..841667fa --- /dev/null +++ b/devdocs/c/variadic%2Fva_arg.html @@ -0,0 +1,54 @@ +

va_arg

Defined in header <stdarg.h>
T va_arg( va_list ap, T );
+

The va_arg macro expands to an expression of type T that corresponds to the next parameter from the va_list ap.

+

Prior to calling va_arg, ap must be initialized by a call to either va_start or va_copy, with no intervening call to va_end. Each invocation of the va_arg macro modifies ap to point to the next variable argument.

+

If the type of the next argument in ap (after promotions) is not compatible with T, the behavior is undefined, unless:

+

If va_arg is called when there are no more arguments in ap, the behavior is undefined.

+

Parameters

+ + +
ap - an instance of the va_list type
T - the type of the next parameter in ap

Expanded value

the next variable parameter in ap

+

Example

#include <stdio.h>
+#include <stdarg.h>
+#include <math.h>
+ 
+double stddev(int count, ...) 
+{
+    double sum = 0;
+    double sum_sq = 0;
+    va_list args;
+    va_start(args, count);
+    for (int i = 0; i < count; ++i) {
+        double num = va_arg(args, double);
+        sum += num;
+        sum_sq += num*num;
+    }
+    va_end(args);
+    return sqrt(sum_sq/count - (sum/count)*(sum/count));
+}
+ 
+int main(void) 
+{
+    printf("%f\n", stddev(4, 25.0, 27.3, 26.9, 25.7));
+}

Output:

+
0.920258

References

See also

+ + + + +
+
(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)
enables access to variadic function arguments
(function macro)
C++ documentation for va_arg
+

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

+
-- cgit v1.2.3