Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
#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);
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.
If successful, vprintf() returns the number of characters written to stdout.
⁄* 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);
}
Mon Tues Wed Thurs Fri