Linux C on the output function definition:
int printf(const char *format,…); int vprintf(const char * format,va_list ap); int vfprintf(FILE *stream,cosnt char *format,va_list ap); int vsprintf(char *str,const char *format,va_list ap); int vsnprintf(char *str,size_t size,const char*format,va_list ap);
va_list ap; // first define a variable of type va_list
void va_start (va_list ap, last); a first variable // va_start va_list parameter, the second parameter is a function of the last fixed parameter
void va_end (va_list ap); // end of the iteration with va_end
type va_arg(va_list ap,type);
Use int_vsnprintf (char * str, size_t size, const char * format, va_list ap); format conversion function output variable parameters of the function.
vsnprintf () c is one of the library functions, are variable parameters, for the character string, and print data and user-defined data format and the like.
Header file: #include <stdio.h>
Function declaration: int_vsnprintf (char * str, size_t size, const char * format, va_list ap);
Parameter Description:
char * str [out]: generating the formatted string stored here.
size_t size [in]: maximum number of bytes str prevent pharmaceutically array bounds.
const char * format: [in] string specifying the output format, which determines the type of the variable parameter, you need to provide a sequence number.
va_list ap [in], va_list variable, va: variable-argument: variable parameter. (... it is the content included in this variable is stored in the variable parameters were to go);
Variable length data reading method
va_start(args,fmt); size_t buf_len = vsnprintf(buffer,MAX_LOG_LEN,str_format,args); va_len(args);
Examples of variable parameters:
/*** vsnprintf.c ***/ #include<stdio.h> #include<stdarg.h> void my_print(char *fmt,...) { va_list args; va_start(args,fmt); char buff[1024]; vsnprintf(buff,1023,fmt,args); printf("%s\n",buff); va_end(args); } int main() { int age = 78; my_print("hello world"); my_print("hello %d",2345); my_print("hello my age is : %d",age); return 0; }
Output:
Bot @ ubuntu: ~ / wangqinghe / C / 20190703 $ gcc vsnprintf.c -that vsnprintf
exbot @ ubuntu: ~ / wangqinghe / C / 20190703 $ ./vsnprintf
hello world
hello 2345
hello my age is : 78