C Programming

Vsnprintf Function in C

Vsnprintf() is a type of function that is used to print the character array using the formatted data in the argument lists. In simple words, this function simply writes the already formatted data that is stored in the variable list “type” arguments into a buffer string. This function composes a string if we use the same format as in the variable argument on the printf() method. But it uses the already identified elements from the arguments in the variable argument list and stores the result in the form of the buffer string in C that is pointed by the pointer of the array where the output prints in the arguments. It also takes the size of the buffer or the capacity of the array to fill the numbers in terms of characters.

Procedure

This article will discuss how we will learn the concept behind the vsnprintf() methods. It will also show the order in which we will get to know about the syntax for this function with the implementation of the function on different examples. Lastly, we will conclude our topic to let us revise what we learned so far in the article regarding the topic – Vsnprintf() function.

Syntax

The vsnprintf function is written and declared in the same as we write any other function. But the argument list for this function varies a bit. The vsnprintf() function with its argument list is given in the following:

$ Int vsnprintf (char * array, size_length_array, const wchar_t *format, va_ list args)

We now discuss the requirement of each argument for this function one by one. The first argument is the pointer that points to the data type “char” array where we want to print the output. The second argument represents the maximum characters that the array can store in it. The third argument represents the format pointer that points to the format in which we want to print the output. And the last one is “va_list args” which is known to be a type which holds the necessary information about the functions’ variable arguments. Here, it acts as a pointer that points to the argument list.

Return Type

The return type of this function is integers. This function returns the characters if and only if the program gets successful without counting the null characters for termination. It might return a negative value in case of the failure of the program.

Example:

We initiate the implementation of the previously-mentioned function, the vsnprintf() function, with the help of this example. We implement this example in the Microsoft Visual Studio in the C programming language. The vsnprintf() function is used to write/print the formatted variable argument list to the array or buffer string. So, we first create a variable argument list in this example and then use this argument list to print a data type “char” array.

To start with an example, create a new project in the Visual Studio. To make this project access the fundamental libraries of the C, add the project to the path of C files by adding the source file “.c” extension with the name of the project and save it. The next and the most basic step while creating a program is to import the special libraries to the program so that you may access the required default functions provided by these libraries later in the program. We import the following two header files for this program:

$ # include <stdio.h>

$ # include <stdarg.h>

The first header file makes sure to include the functions for the printf() and scanf(). The second one is the header file that we specially imported to use the vsnprintf() method after creating the variable argument list. After including the previous two header files in the project, we are ready to implement the part of our program that deals with the building of code for our example. We create a function with the return type of integer “int” and name this function to be “formattedlist”. This function replicates all the parameters that we already discussed in this article for the vsnprintf() function as the arguments of the function. The first parameter that we pass to this function is the pointer to the array where the output is written and then printed. Then, we give the size of this array and pass the format in which we print our array in this function.

We first initialize the length of the buffer to be “0”. Then, we create or initialize a variable argument list for this function with the type “va_list” and name “arguments”. Then, we start this va_ list by calling the “va_start()” and pass it the format and the arguments as its parameters. We then define the length for the buffer by calling the vsnprintf() and pass all the objects for the pointers that we defined in the parameters of the “formattedlist” function. We end the argument list by calling the va_end() and come out of the function by returning the length.

Now, we declare the main function. Into this main function, we declare a “buffer” array with the size of “50”. Then, we create the argument array of size “50” and assign it with the characters “ you did well”. Then, we initialize the length in this main function again and call the formattedlist() function and pass it the parameters according to the parameters that we defined earlier for the length while creating the “formattedlist function”. This time, we pass the “arguments” as a parameter to this function. Then, we display this length and the array by calling the printf() method as shown in the following code snippet:

# include <stdio.h>

# include <stdarg.h>

int formattedlist(char* buffer, int bufferSize, const char* format, ...)

{

    int length = 0;m
    va_list arguments;
    va_start(arguments, format);
    length = vsnprintf(buffer, bufferSize, format, arguments);
    va_end(arguments);
    return length;
}

int main()
{
    char buffer[50];
    char arguments[50] = "you did well";

    int length = formattedlist(buffer, 50, "%s ", arguments);

    printf(" array: %s \n", buffer);
    printf("Numbers in array: %d \n", length);

    return 0;


}


After the execution of the previously-mentioned program, it first composed a formatted string and printed this formatted string in the form of the string buffer or character array.

Conclusion

This article guides us on how we can use the concept of the vsnprinft() function to print the data type character “char” array from the list of arguments that are already formatted. We have done an example on this function to clear out the thoughts and have a hands-on experience on this topic.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.