Syntax
#include <stdarg.h> #include <stdio.h> int vsprintf(char *target-string, const char *format, va_list arg_ptr);Description
vsprintf formats and stores a series of characters and values in the buffer target-string. vsprintf works just like sprintf, except that arg_ptr points to a list of arguments whose number can vary from call to call in the program. These arguments should be initialized by va_start for each call. In contrast, sprintf can have a list of arguments, but the number of arguments in that list is fixed when you compile the program.
vsprintf converts each entry in the argument list according to the corresponding format specifier in format. The format has the same form and function as the format string for printf. For a description of the format string, see printf.
In extended mode, vsprintf also converts floating-point values of NaN and infinity to the strings "NAN" or "nan" and "INFINITY" or "infinity". The case and sign of the string is determined by the format specifiers. See Infinity and NaN Support for more information on infinity and NaN values.
If successful, vsprintf returns the number of bytes written to target-string. If an error occurs, vsprintf returns a negative value.
This example assigns a variable number of strings to string and prints the resultant string.
#include <stdarg.h> #include <stdio.h> void vout(char *string, char *fmt,...); char fmt1[] = "%s %s %s\n"; int main(void) { char string[100]; vout(string, fmt1, "Sat", "Sun", "Mon"); printf("The string is: %s", string); return 0; /**************************************************************************** The output should be: The string is: Sat Sun Mon ****************************************************************************/ } void vout(char *string, char *fmt,...) { va_list arg_ptr; va_start(arg_ptr, fmt); vsprintf(string, fmt, arg_ptr); va_end(arg_ptr); }Related Information