vsprintf() — Format and print data to buffer

Standards

Standards / Extensions C or C++ Dependencies

ISO C
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <stdarg.h>
#include <stdio.h>

int vsprintf(char *  __restrict__target-string, 
             const char *  __restrict__format, va_list arg_ptr);

General description

The vsprintf() function is similar to sprintf(), except that arg_ptr points to a list of arguments whose number can vary from call to call in the program. In contrast, sprintf() can have a list of arguments, but the number of arguments in that list is fixed when you compile the program. For a specification of the format string, see fprintf(), printf(), sprintf() — Format and write data.

Returned value

If successful, vsprintf() returns the number of characters written target-string.

If unsuccessful, vsprintf() returns a negative value.
Note: In contrast to some UNIX-based implementations of the C language, the z/OS® XL C/C++ implementation of the vprintf() family increments the pointer to the variable arguments list. To control whether the pointer to the argument is incremented, call the va_end macro after each call to vsprintf().

Example

CELEBV05
⁄* CELEBV05                                      

   This example assigns a variable number of strings to string                  
   and prints the resultant string, using &vsprintf..                           
                                                                                
 *⁄                                                                             
#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);                                      
}                                                                               
void vout(char *string, char *fmt, ...)                                         
                                                                                
{                                                                               
   va_list arg_ptr;                                                             
                                                                                
   va_start(arg_ptr, fmt);                                                      
   vsprintf(string, fmt, arg_ptr);                                              
   va_end(arg_ptr);                                                             
}                                                                               
                                                                                
Output
The string is:  Sat  Sun  Mon

Related information