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:
void
and the other is a pointer to a character type. If va_arg
is called when there are no more arguments in ap
, the behavior is undefined.
ap | - | an instance of the va_list type |
T | - | the type of the next parameter in ap |
the next variable parameter in ap
.
#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
(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