Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

vfprintf — formatted output from va_list

vfprintf writes formatted data to a FILE stream, taking arguments from a va_list rather than a variable argument list.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdarg.h>
#include <stdio.h>
int vfprintf(FILE * restrict stream, const char * restrict format, va_list arg);

vfprintf works like fprintf, but accepts a va_list. The headers are <stdio.h> and <stdarg.h>; formatting rules remain the same.

Initialize the list with va_start. After the call, do not reuse it except for va_end; for an independent traversal, first create a copy with va_copy and end that copy with va_end too.

The result is the number of characters written, or a negative value on output or encoding error. Argument types must match the format; mismatches cause undefined behavior. Pass untrusted text through %s, not as the format.

Examples

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

static int write_message(FILE *stream, const char *format, ...)
{
    va_list args;
    va_start(args, format);
    int result = vfprintf(stream, format, args);
    va_end(args);
    return result;
}

int main(void)
{
    int count = write_message(stdout, "%s: %d\n", "Items", 3);
    int flush_result = fflush(stdout);
    return count == 9 && flush_result == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

Result

Items: 3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources