C programming stdio.h function - int vsprintf(char *str, const char *format, va_list arg)

The vsprintf function in the stdio.h library of C programming language is used to write formatted data to a character string. It is similar to the sprintf function, but takes a va_list argument instead of a variable number of arguments.

The syntax of the vsprintf function is as follows:

int vsprintf(char *str, const char *format, va_list arg);
S‮cruo‬e:www.theitroad.com

Here, str is a pointer to the character string where the output will be stored, format is a string that specifies the format of the output, and arg is a va_list variable that contains the arguments to be formatted.

The function returns the number of characters written to the string, not including the null terminator.

Here's an example that uses vsprintf to format a string:

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

int main() {
    char buffer[100];
    int n = 42;
    const char* str = "Hello, world!";

    vsprintf(buffer, "n = %d, str = %s", n, str);
    printf("%s\n", buffer);
    return 0;
}

This program will output:

n = 42, str = Hello, world!