vprintf() Function
The vprintf() function in C stdio.h is used to print formatted data to the standard output using a variable argument list. It functions similarly to printf() but retrieves its arguments from a list initialized with va_start(), making it ideal for cases where the number and types of arguments are not fixed at compile time.
Syntax of vprintf()
int vprintf(const char *format, va_list arg);
Parameters
| Parameter | Description |
|---|---|
format | A C string that contains the format to be used, following the same rules as printf(). |
arg | A variable argument list (of type va_list) that has been initialized with va_start() and contains the values to be printed. |
Before calling vprintf(), ensure that the variable argument list is properly initialized using va_start() and released using va_end() after the call.
Return Value
On success, vprintf() returns the total number of characters written to the standard output. In case of an error, such as a writing error or a multibyte character encoding error, a negative number is returned, with the error indicator set accordingly and errno possibly set to EILSEQ.
Examples for vprintf()
Example 1: Basic Usage of vprintf() for Formatted Output
This example demonstrates how to use vprintf() to print a formatted string by passing a variable argument list.
Program
#include <stdio.h>
#include <stdarg.h>
void printFormatted(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main() {
printFormatted("Name: %s, Age: %d\n", "Alice", 30);
return 0;
}
Explanation:
- The
printFormatted()function accepts a format string followed by a variable number of arguments. - The variable argument list is initialized using
va_start()and then passed tovprintf()for printing. - The variable argument list is properly cleaned up using
va_end(). - The
main()function callsprintFormatted()with a format string and corresponding arguments, resulting in formatted output.
Program Output:
Name: Alice, Age: 30
Example 2: Using vprintf() in a Custom Logging Function
This example illustrates how vprintf() can be utilized within a custom logging function to handle formatted messages with variable arguments.
Program
#include <stdio.h>
#include <stdarg.h>
void logMessage(const char *format, ...) {
va_list args;
va_start(args, format);
printf("LOG: ");
vprintf(format, args);
va_end(args);
}
int main() {
logMessage("Error code %d occurred at %s\n", 404, "main()");
return 0;
}
Explanation:
- The
logMessage()function is designed as a custom logger that prefixes messages with “LOG: “. - The variable argument list is initialized using
va_start()and passed tovprintf()for formatted output. - After printing, the variable argument list is cleaned up with
va_end(). - The
main()function demonstrates logging an error message with a specific error code and location.
Program Output:
LOG: Error code 404 occurred at main()
Example 3: Handling Multiple Data Types with vprintf()
This example demonstrates using vprintf() to print a formatted string containing various data types, showcasing its versatility.
Program
#include <stdio.h>
#include <stdarg.h>
void printDetails(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main() {
printDetails("Integer: %d, Floating: %.2f, String: %s\n", 42, 3.14, "Test");
return 0;
}
Explanation:
- The
printDetails()function accepts a format string with placeholders for an integer, a floating-point number, and a string. - The variable argument list is initialized with
va_start()and processed byvprintf()to print the formatted output. - The
main()function callsprintDetails()with appropriate arguments, resulting in the display of various data types.
Program Output:
Integer: 42, Floating: 3.14, String: Test
