Vsnprintf,c one of the language library functions, which belongs to a mutable parameter. Used to print data, data format user customizations to a string.
Header file:
#include <stdarg.h>
function declaration:
int_vsnprintf (CHAR*STR,SIZE_TSIZE,CONSTCHAR*FORMAT,VA_LISTAP);
Parameter description:
Char *str [out], storing the generated formatted string here.
size_t size [in], the maximum number of bytes that STR can accept to prevent an array from being out of bounds.
const char *format [in], specifying the output format string, which determines the type, number, and order of the mutable arguments you need to provide.
Va_list ap [in], va_list variable. Va:variable-argument: Variable Parameters
function function: format the variable parameter to an array of characters.
The usage is similar to vsprintf, but the size limit is added to prevent memory overflow (size is the amount of storage space referred to by STR).
return value: execution succeeds, returns the number of characters written to the character array str (does not contain a terminator), the maximum size is not exceeded, the execution fails, a negative value is returned, and the errno is reset. [1]
Note:
Linux environment is: vsnprintf
VC6 Environment is: _vsnprintf
---
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
static const int max_log_length = 4*1024;
static void Print_log (const char * format, ...)
{
va_list args;
Va_start (args, format);
Char Buf[max_log_length];
vsnprintf (buf, max_log_length-3, format, args);
strcat (buf, "\ n");
fprintf (stdout, "log:%s", buf);
Fflush (stdout);
Va_end (args);
}
int main () {
Print_log ("%d%s", 111000, "test");
return 0;
}
-------
Reference: http://stackoverflow.com/questions/1056411/how-to-pass-variable-number-of-arguments-to-printf-sprintf