vsprintf -- Print Argument Data to Buffer

Format

#include <stdarg.h>
#include <stdio.h>
int vsprintf(char *target-string, const char *format, va_list arg_ptr);

Language Level: ANSI, XPG4, Extension
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 -- Print Formatted Characters.

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.

If you specify a null string for the %s or %ls format specifier, vsprintf prints (null). (In previous releases of VisualAge for C++, vsprintf produced no output for a null string.)

Return Value
If successful, vsprintf returns the number of bytes written to target-string. If an error occurs, vsprintf returns a negative value.

Example
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\n", 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);
}


printf -- Print Formatted Characters
sprintf -- Print Formatted Data to Buffer
swprintf -- Format and Write Wide Characters to Buffer
va_arg - va_end - va_start -- Access Function Arguments
vfprintf -- Print Argument Data to Stream
vprintf -- Print Argument Data
vswprintf -- Format and Write Wide Characters to Buffer
<stdarg.h>
<stdio.h>