C library macro - va_start ()
C standard library - <stdarg.h>
description
C library macrovoid va_start (va_list ap, last_arg) apinitialized variable, itva_argandva_endmacros are used together.last_arg passed to the function is the last known fixed parameter, the ellipsis previous parameters.
This macro must be called before usingva_arg and va_end.
statement
Here is the va_start () macro statement.
void va_start(va_list ap, last_arg);
parameter
- ap - it is an object of type va_listwhich is used to store additional parameters through theva_argget the necessary information.
- last_arg - last function known fixed parameter passed to it.
return value
NA
Examples
The following example demonstrates the va_start () macro usage.
#include<stdarg.h> #include<stdio.h> int sum(int, ...); int main(void) { printf("10、20 和 30 的和 = %d\n", sum(3, 10, 20, 30) ); printf("4、20、25 和 30 的和 = %d\n", sum(4, 4, 20, 25, 30) ); return 0; } int sum(int num_args, ...) { int val = 0; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) { val += va_arg(ap, int); } va_end(ap); return val; }
Let's compile and run the above program, which will result in the following:
10、20 和 30 的和 = 60 4、20、25 和 30 的和 = 79