CKTIS / C
vprintf — formatted output from a va_list
vprintf writes formatted text to stdout using arguments from a va_list. It supports custom output functions that accept a variable number of arguments.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdarg.h>
#include <stdio.h>
int vprintf(const char * restrict format, va_list arg);vprintf behaves like printf, but accepts a va_list instead of arguments through .... It writes to stdout; vfprintf lets you select a stream.
Initialize the list with va_start. After the call, do not reuse it without reinitialization; call va_end. For an independent traversal, first use va_copy, then a separate va_end.
If any arguments remain after the entire format has been processed, the excess arguments are ignored. Passing too few arguments or passing an argument whose type after the default argument promotions does not match the type required by its conversion specification causes undefined behavior. Pass untrusted text as data for %s, not as the format.
The return value counts transmitted characters, or is negative for an output or encoding error. The example expects 10, including \n.
Examples
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
static int print_message(const char *format, ...)
{
va_list args;
va_start(args, format);
int result = vprintf(format, args);
va_end(args);
return result;
}
int main(void)
{
int count = print_message("%s: %d\n", "Score", 42);
if (count != 10) {
return EXIT_FAILURE;
}
if (fflush(stdout) == EOF) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}Result
Score: 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.