C Programming

sprintf Function in C

In this Linux Hint tutorial, we will discuss the working of the sprintf() function in the C programming language. The sprintf() function is similar to the printf() function, but the main difference is printf() function is used to send the string to stdout output. The sprintf() function directs the output to a string variable in memory that can be used for various purposes including further string processing or writing to a file or stdout.

Syntax of sprint() Function in C:

In the sprintf() function, we have declared the type ”int”. There is a parameter name buffer of a type character that is a pointer used to store the character string in the large-size buffer. The argument *format is the string that is used to describe the output.

Specifiers Used in sprintf() Function in C

The following are the different format specifiers used in C language to determine the variable type which we want to display on the output screen:

Format Specifiers Explanation
%d Represents an integer number.
%f Represents a fixed decimal float value.
%.1f Represents a value in floating-point with one digit preceding the decimals
%e Represents a decimal float value in scientific notation(exponential).
%g Represents a floating-point value either in the static decimal or exponential format based on the length of the value.
%c Represents character variables.
%s Represents a character string.
%p Points to the address of the pointer.
%n Prints nothing.

Returned Value of sprintf() Function

The entire number of characters printed, except the empty character inserted at the ending of the string, is reverted if successfully compiled. Alternatively, a negative value is returned in the event of failure.

Implementation Of sprintf() Function

Use C online compiler or Dev C++ compiler for the execution of the sprintf() function in C programming language.

Example 01:

The following is the simplest illustration to show the function of the C programming language’s sprintf() function. In this case, by multiplying “x” and “y,” we can determine the value of “z”. To start writing our first program, we must have to include the header files to execute the program. The “stdio.h” is used to get the input from the user and displays the output after the compilation of the program. The header file “stdlib.h” stands for the standard library that contains methods for data storage, control activities, calculations, and other things.

Then, we started implementing the main() method that serves as the beginning of the implementation of the program’s code in C. In language C, a main is a standardized keyword or method. The main() function is the first method in-charge of launching the execution of the code and then shutting down the program. The main() method has an “int” return data type which always initiates execution from the “main” function.

Then, we have declared a variable named “x” with a data type “int” known as an integer. The “printf()” method is called to display the string exactly that was written in the quotation marks (i.e. Enter the Value of x:). Then, we have to get the input from the user. So, we have used the “scanf()” method. In the “scanf()” method, the “%d” specifier is used for the integer type variable “x” to display on the screen. Same as it is, we have declared the variable “y” with data type “int” and got the input from the user.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x;
    printf("Enter the value of x: ");
    scanf("%d", &x);

    int y;
    printf("Enter the value of y: ");
    scanf("%d", &y);

    int z= x*y;
    char buffer[50];

    sprintf(buffer, "The Multiplication of %d and %d is: %d", x, y, z);
    printf("%s\n", buffer);

    return 0;
}

We have declared another variable “z” that was used to store the answer of the multiplication “x * y” in it with the data type “int”. After declaring all the valid variables, we have declared a character type “buffer” of length 50. Furthermore, by employing “%d” specifiers, the sprintf() method enables the construction of strings without instantaneously displaying the result of multiplication. Then, print the character string that was written in double quotation marks. At the end of the program, return the 0 to the main() function that will show the termination of the program execution

Here is the output of the above illustration. First, you have to enter the value of “x” and the value of “y”. The sprintf() method would then be used by the translator to show the outcome of multiplying the two values.

Example 02:

In this second illustration from our article, we calculated the value of PI by inputting the circumference and diameter of a circle. Let’s begin writing the program that will calculate the value of PI.

To start writing the program, we must first have to include the header file. In C language, the header file has the extension “.h”. The “stdio.h”, “stdlib”, and “math.h” header files are required for our application to run. The header file “stdio.h” is used to display the input and output of the program with the preprocessor directive “#include”. The primary code for our program, which we intend to execute and produce the appropriate output, is written in the main() body.

In the main() function body, we have declared two “int” variables, “circumference” and “radius,” as well as a “float” variable which is the “diameter”, to determine the surface area of the circle. The float value of “pi” was then saved in an additional variable called “pi.” Lastly, the “buffer” of the type character holds the string using a length of 50. While allocating resources, the buffer was retrieving the characters that were written and attaching them to a string after obtaining all of the variables. The main() method attempts to comprehend each variable. If the implementation is carried out correctly, it will then return 0 to the main() method.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main() {

    int circumference= 44;

    printf("The Value of circumference is: %d \n", circumference);

    int radius=7;

    printf("To Find value of Pi. First, find the value of diameter. \n");

    float diameter= (float) 7*2;

    printf("Multiplying the value of Radius by 2 to get the value of diameter.\n\n"
              "The value of diameter is: %f \n",diameter);

    float pi=  circumference/diameter;

    char buffer[50];
    sprintf(buffer, "%f", pi);

    printf("The value of Pi is stored as %s\n", buffer);

    return 0;
}

Following the execution of the aforementioned code snippet, we were able to determine the value of “pi” using the circle’s circumference and diameter.

Conclusion

The sprintf() function of the programming language C was addressed in this Linux Hint tutorial. We’ve talked about the sprintf() function’s syntax and the format specifiers that were employed while coding in C to declare the parameter. Then, to help the user understand how the sprintf() method operates, we implemented two unique examples.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content