Va_list is a set of macros used to solve variable parameters in C language. It is defined in the header file <stdarg. h>.
Usage of va_list:
(1) first define a va_list variable in the function. This variable is a pointer to the parameter.
(2) then use the va_start macro to initialize the va_list variable just defined in the variable. The second parameter of this macro is the first parameter of the first variable parameter and is a fixed parameter.
(3) then use va_arg to return variable parameters. The second parameter of va_arg is the type of the parameter to be returned.
(4) use the va_end macro to end the variable parameter acquisition. Then you can use the second parameter in the function. If a function has multiple variable parameters, call va_arg to obtain the parameters.
#include<stdio.h>#include <stdarg.h>void arg_cnt(int cnt, ...){ int value=0; int i=0; int arg_cnt=cnt; va_list arg_ptr; va_start(arg_ptr, cnt); for(i = 0; i < cnt; i++) { value = va_arg(arg_ptr,int); printf("value%d=%d\n", i+1, value); } va_end(arg_ptr);}int main(void){ arg_cnt(5,1,2,3,4,5); return 0;}
Run the above program:
Value1 = 1
Value2 = 2
Value3 = 3
Value4 = 4
Value5 = 5
Have you found any inconvenience to this program?
Yes! The number of variable parameters must be specified in the first parameter, that is, the CNT variable in the arg_cnt function,
If I call
arg_cnt(6,1,2,3,4,5);
The result is unpredictable.
The following describes a vsnprint function that supports variable parameters:
Header file: # include <stdarg. h> function declaration: int vsnprintf (char * buffer, size_t max_count, const char * format, va_list varglist); parameter description: char * buffer [out], store the formatted strings here. size_t max_count [in], the maximum number of bytes acceptable for buffer, to prevent array overflow. const char * Format [in], format the string va_list varglist [in], va_list variable. VA: Variable-argument: Variable Parameter usage is similar to vsprintf, but max_count restrictions are added.
Return Value Description: If this function is successfully called, the number of characters written to the buffer (excluding '\ 0' at the end) is returned '). The snprintf and vsnprintf functions cannot write more bytes than the size (including the ending '0. If the output is truncated because of the preceding reasons, the number of characters (not including the ending '\ 0') that are successfully written into the buffer is returned, if there is enough memory space. Therefore, if the returned value is equal to or greater than size, it indicates that the characters output to buffer are truncated. If an error occurs during the output process, a negative number is returned.
#include<stdio.h>#include <stdarg.h>#define bufsize 80char buffer[bufsize];int vspf(char *fmt, ...){ va_list argptr; int cnt; va_start(argptr, fmt); cnt = vsnprintf(buffer,bufsize ,fmt, argptr); va_end(argptr); return(cnt);}int main(void){ int inumber = 30; float fnumber = 90.0; char string[4] = "abc"; int cnt = 0; cnt = vspf("%d %f %s", inumber, fnumber, string); printf("%s\n", buffer); printf("cnt=%d\n", cnt); return 0;}
The execution result is as follows: 30 90.000000 abccnt = 16