vsprintf() Function

The vsprintf() function in C stdio.h formats data from a variable argument list into a string, composing the same output that would be generated by printf() but storing it in a user-provided buffer. This function is particularly useful when the number or types of arguments are determined at runtime.


Syntax of vsprintf()

</>
Copy
int vsprintf(char *s, const char *format, va_list arg);

Parameters

ParameterDescription
sPointer to a buffer where the resulting C-string is stored. The buffer must be large enough to hold the formatted output.
formatA C string that contains a format string following the same specifications as in printf().
argA variable argument list of type va_list, which should be initialized using va_start before the call and cleaned up with va_end after.

Internally, vsprintf() retrieves the arguments from the provided va_list as if using va_arg, and in doing so, it alters the state of the argument list. It is crucial to ensure that the va_list is properly managed by initializing it with va_start and finalizing it with va_end to avoid undefined behavior.


Return Value

On success, vsprintf() returns the total number of characters written to the buffer, excluding the terminating null byte. On failure, a negative number is returned.


Examples for vsprintf()

Example 1: Basic Usage of vsprintf() to Format a String

This example demonstrates how to use vsprintf() within a helper function that accepts variable arguments to format a string.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

void demo_vsprintf(char *buffer, const char *format, ...) {
    va_list args;
    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
}

int main() {
    char buffer[100];
    demo_vsprintf(buffer, "Value: %d, Pi: %.2f", 42, 3.14159);
    printf("%s\n", buffer);
    return 0;
}

Explanation:

  1. A helper function demo_vsprintf() is defined to encapsulate the usage of vsprintf().
  2. The function initializes a va_list with va_start, formats the string using vsprintf(), and then cleans up with va_end.
  3. In the main() function, the helper is called with a format string and corresponding arguments.
  4. The resulting formatted string is printed to the console.

Program Output:

Value: 42, Pi: 3.14

Example 2: Formatting a String with Multiple Data Types

This example illustrates how vsprintf() can be used to format a string containing various data types including strings, integers, and floating-point numbers.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

void format_string(char *buffer, const char *format, ...) {
    va_list args;
    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
}

int main() {
    char buffer[200];
    format_string(buffer, "Name: %s, Age: %d, GPA: %.2f", "John Doe", 25, 3.75);
    printf("%s\n", buffer);
    return 0;
}

Explanation:

  1. A function format_string() is defined to process a variable number of arguments using vsprintf().
  2. The function formats a string based on a format specifier that includes a string, an integer, and a floating-point number.
  3. In the main() function, the formatted string is constructed and then printed.

Program Output:

Name: John Doe, Age: 25, GPA: 3.75

Example 3: Implementing a Custom Logging Function with vsprintf()

This example demonstrates how to integrate vsprintf() within a custom logging function to format log messages dynamically.

Program

</>
Copy
#include <stdio.h>
#include <stdarg.h>

void log_message(const char *format, ...) {
    char buffer[256];
    va_list args;
    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);
    printf("LOG: %s\n", buffer);
}

int main() {
    log_message("Error %d: %s", 404, "Not Found");
    return 0;
}

Explanation:

  1. A logging function log_message() is defined to accept a format string and variable arguments.
  2. The function uses vsprintf() to format the log message into a buffer.
  3. The formatted message is then printed to the console with a “LOG:” prefix.
  4. In the main() function, the logging function is invoked with an error code and a corresponding message.

Program Output:

LOG: Error 404: Not Found