In the C programming language, you will come across many conversions and copy data from one file or one variable to another, like the conversion from int to string, character array to string, etc. A similar type of conversion will be discussed herein in which the characters from the format strings are written to the buffer. And the values will be obtained through that buffer variable. This is done through a function in C known as sprints().

Syntax of sprintf()

# int sprintf (char* buffer, const char* variable);

A variable is an input value that we want to store in the buffer. The sprintf function, when executed, returns the number of characters that are present in the buffer. Otherwise, a negative value is returned. The size of the buffer is declared at the time of initialization; the size should be large enough to store a string in it.

Implementation of sprintf()

To understand the working of sprintf, see the below examples that contain different methods of using this function in the source code of C so that the compiler can execute it in the Ubuntu terminal.

Example 1

The implementation of the sprintf contains a sample example here. The main function will be involved in calculating the value of pi, an absolute value in mathematics. For this purpose, we have to use the math library containing all the math operations’ features. The second library is stdio.h, which is also an essential header file that is required to execute any C program.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-27.png" data-lazy- height="203" src="data:image/svg xml,” width=”386″>

A character array is initialized with a fixed size in the main program. The variable will be used in the sprint() function as a parameter. As sprint is used to copy or write the formatted string to the buffer of the character string, inside the function, we will use the object array of the character variable and the pi mathematics calculator.

# Sprint (str, “value of pi = %f”, M_PI);

This will bring the value of PI. The buffer value of the sprint function is used to display the value. As in this example, str is used.

The code is compiled and executed by a compiler of C , which is a g compiler. This compiler uses an input file that has the source code you have saved, an output file in which we want to display the results by using ‘-o’ in the command.

$ g o file file.c

$ ./file

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image8-18.png" data-lazy- height="58" src="data:image/svg xml,” width=”500″>

On the execution, you will see that the value of pi is displayed through the sprint function.

Example 2

This is another instance of the sprintf() function. A character variable is initialized to convert the string. Another integer type variable is also used here, to which an integer value is assigned. Both variables will be used in the parameter of the sprint function. The sprintf function will print my age sentence, and the age variable will be converted to the buffer variable.

# Sprintf (buffer, “My age is %d”, age);

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-23.png" data-lazy- height="285" src="data:image/svg xml,” width=”547″>

To display the converted value, we will display the contents of the buffer by using a buffer.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image10-12.png" data-lazy- height="57" src="data:image/svg xml,” width=”483″>

When the file is executed, you will see that the buffer will display the initialized value as an integer value.

Example 3

We will check that all the elements should be present in the buffer. In the main program, two variables of different data types, one integer, and one character, are created and are initialized by the name and age. An integer type variable is used here for the counting purpose of characters. Here we will write the combination of the variables to buffer variables and strings. The combination will be done in such a way that a sentence is written in the cout statement along with ‘%s’ and ‘%d’ variables; these two will contain the values of both the integer and character variables initialized earlier.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image9-14.png" data-lazy- height="400" src="data:image/svg xml,” width=”635″>

In this way, both the string and the variables will be written collectively to the buffer variable. Now the count function will be used with the function call. It will store the value that will be returned from the function sprint.

# Count = sprint (buffer, ‘ I am %s and I am %d years old, name, age);

After the function, the buffer is displayed to show the value written in it. Furthermore, the count function has counted the total characters that will be formed when the strings and both variables are combined. Now we will execute the above source code.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-29.png" data-lazy- height="58" src="data:image/svg xml,” width=”417″>

You will see that the sprint function has displayed the message having the name and age embedded inside the sentence. The counter has also displayed the total number of characters, including all the characters of strings, both the variables and the space between each word.

Example 4

This example works on the same approach as we have used for the previous example; all the strings in the statement and the variables are combined with being written in the buffer variable. A character buffer array is created, and the size is mentioned here. Two integer-type variables will store the values, and a third variable will calculate the sum of the values present in the two variables. Now we will embed all variables in the string.

# Sprint (buffer, “sum of %d and %d is %d”, a,b,c );

The sprint function will use the three random variables to assign the values. One thing that should be retained in mind is that the order of the variables should be the same. The sum value will be stored into the buffer variable instead of printing through stdout.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-31.png" data-lazy- height="319" src="data:image/svg xml,” width=”516″>

Execute the code; the values are attached with the string through sprint.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-27.png" data-lazy- height="58" src="data:image/svg xml,” width=”480″>

Example 5

The example deals with user involvement. Instead of taking the variables as input and the string statement at the time of executing the sprint, we have taken the string as an input, and the value will come from the user. An original string character array is introduced here that contains a string. Whereas a buffer is also initialized with the specific size, the system will ask the user to enter the value in integer; this value will then be used to be embedded with the string we have taken.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-28.png" data-lazy- height="290" src="data:image/svg xml,” width=”430″>

After the value is entered, this will be now used in the sprint function.

# Sprint (buffer, “%s_%d”, original, n);

Both the original statement and the integer value will be concatenated through the underscore instead of having a space between them. Now execute the statement to view the results.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-27.png" data-lazy- height="96" src="data:image/svg xml,” width=”484″>

After entering the value by the user, the statement will be displayed as the combination of a string and an integer value.

Conclusion

The sprint() method stores the formatted string into the buffer. All the values are written in the buffer either separately or collectively. All these cases are explained in this article through basic examples implemented in C . The verification of the buffer value can be obtained through the counter function that counts the characters of the string. This can be applied to any sprint program.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Author-Image-150×150.jpg6258ebd7efc98.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.