vprintf() — Format and print data to stdout

Standards

Standards / Extensions C or C++ Dependencies

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

both  

Format

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

int vprintf(const char *  __restrict__format, va_list arg_ptr);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int vprintf_unlocked(const char *  __restrict__format, va_list arg_ptr);

General description

The vprintf() function is similar to printf(), 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, printf() 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.

vprintf() is not supported for files opened with type=record or type=blocked.

vprintf() has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

vprintf_unlocked() is functionally equivalent to vprintf() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If successful, vprintf() returns the number of characters written to stdout.

If unsuccessful, vprintf() 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 vprintf().

Example

CELEBV04
⁄* CELEBV04                                      

   This example prints out a variable number of strings to stdout,              
   using &vprintf..                                                             

 *⁄                                                                             
#include <stdarg.h>                                                             
#include <stdio.h>                                                              
                                                                                
void vout(char *fmt, ...);                                                      
char fmt1 [] = "%s  %s  %s   %s   %s \n";                                       
                                                                                
int main(void)                                                                  
{                                                                               
   FILE *stream;                                                                
   stream = fopen("myfile.dat", "w");                                           
                                                                                
   vout(fmt1, "Mon", "Tues", "Wed", "Thurs", "Fri");                            
}                                                                               
                                                                                
void vout(char *fmt, ...)                                                       
{                                                                               
   va_list arg_ptr;                                                             
                                                                                
   va_start(arg_ptr, fmt);                                                      
   vprintf(fmt, arg_ptr);                                                       
   va_end(arg_ptr);                                                             
}                                                                               
                                                                                
Output
Mon  Tues  Wed   Thurs   Fri

Related information