vfprintf() Function
The vfprintf()
function in C stdio.h writes formatted data to a specified output stream using a variable argument list. It operates similarly to printf()
by replacing format specifiers with corresponding values from the variable argument list, making it especially useful for functions that accept a variable number of arguments.
Syntax of vfprintf()
int vfprintf(FILE *stream, const char *format, va_list arg);
Parameters
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the output stream. |
format | C string that contains the format string, following the same specifications as in printf() . |
arg | A va_list initialized with va_start that holds the variable arguments. |
Note: The vfprintf()
function retrieves arguments from the va_list
as if va_arg
were used, thereby altering its state. It is essential that the va_list
is initialized with va_start
before calling vfprintf()
and properly released using va_end
after the call.
Return Value
On success, vfprintf()
returns the total number of characters written. On error, a negative number is returned, the error indicator for the stream is set, and if a multibyte character encoding error occurs while writing wide characters, errno
is set to EILSEQ
.
Examples for vfprintf()
Example 1: Using vfprintf() in a Custom Print Function
This example demonstrates how to create a custom print function that leverages vfprintf()
to write formatted output to a stream.
Program
#include <stdio.h>
#include <stdarg.h>
void my_print(FILE *stream, const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stream, format, args);
va_end(args);
}
int main() {
my_print(stdout, "Number: %d, String: %s\n", 42, "Hello");
return 0;
}
Explanation:
- A custom function
my_print
is defined to accept a variable number of arguments. - The variable argument list is initialized using
va_start
. vfprintf()
is used to write the formatted data to the specified stream.- The variable argument list is cleaned up with
va_end
after printing.
Program Output:
Number: 42, String: Hello
Example 2: Writing Formatted Output to a File Using vfprintf()
This example shows how to use vfprintf()
to write formatted data to a file by wrapping the call in a helper function.
Program
#include <stdio.h>
#include <stdarg.h>
void file_print(const char *filename, const char *format, ...) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
return;
}
va_list args;
va_start(args, format);
vfprintf(fp, format, args);
va_end(args);
fclose(fp);
}
int main() {
file_print("output.txt", "Pi: %.2f\n", 3.14159);
return 0;
}
Explanation:
- The
file_print
function opens a file for writing using the provided filename. - A variable argument list is initialized and passed to
vfprintf()
to write formatted output to the file. - The file is closed after writing to ensure data is saved properly.
Program Output:
Pi: 3.14
Example 3: Logging Error Messages to Standard Error with vfprintf()
This example demonstrates how to use vfprintf()
to log formatted error messages to the standard error stream.
Program
#include <stdio.h>
#include <stdarg.h>
void log_message(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
}
int main() {
log_message("Error: %s occurred at line %d\n", "Segmentation fault", 27);
return 0;
}
Explanation:
- A helper function
log_message
is defined to log messages to the standard error stream. - The function initializes a variable argument list and passes it to
vfprintf()
for formatted output. - The error message is printed to
stderr
and the argument list is subsequently cleaned up withva_end
.
Program Output:
Error: Segmentation fault occurred at line 27