Defined in header <stdarg.h> | ||
T va_arg( va_list ap, T); | ||
Theva_arg macro expands to an expression of typeT that corresponds to the next parameter from theva_listap.
Prior to callingva_arg,ap must be initialized by a call to eitherva_start orva_copy, with no intervening call tova_end. Each invocation of theva_arg macro modifiesap to point to the next variable argument.
If the type of the next argument inap (after promotions) is notcompatible withT, the behavior is undefined, unless:
Ifva_arg is called when there are no more arguments inap, the behavior is undefined.
Contents |
| ap | - | an instance of theva_list type |
| T | - | the type of the next parameter inap |
the next variable parameter inap
#include <math.h>#include <stdarg.h>#include <stdio.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);returnsqrt(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)[edit] |
| ends traversal of the variadic function arguments (function macro)[edit] | |
| holds the information needed byva_start,va_arg,va_end, andva_copy (typedef)[edit] | |
| enables access to variadic function arguments (function macro)[edit] | |
C++ documentation forva_arg | |