Home » sprintf() in C

sprintf() in C

sprintf stands for “string print”. In C programming language, it is a file handling function that is used to send formatted output to the string. Instead of printing on console, sprintf() function stores the output on char buffer that is specified in sprintf.

Syntax

Parameter values

The sprintf() function accepts some parameter values that are defined as follows –

str: It is the pointer to an array of char elements where the resulting string is stored. It is the buffer to put the data in.

format: It is C string that is used to describe the output along with placeholders for the integer arguments to be inserted in the formatted string. It is said to the string that contains the text to be written to buffer. It consists of characters along with the optional format specifiers starting with %.

Now, let’s see some examples of using the sprintf() function in C.

Example1

This is a simple example to demonstrate the use of sprintf() function in C. Here, we are using multiple arguments with the sprintf() function.

Output:

The Sum of 15 and 25 is 40  

Example2

This is another simple example to demonstrate the use of sprintf() function in C. In this example, there is a variable num which is float type. The sprintf() function convert the data of num variable into string and the resulting string will be stored in buffer.

Output:

Before using sprintf(), data is float type: 9.900000  After using sprintf() data is string type: 9.900000  

Example3

This is another example of using the sprintf() function in C.

Output:

Value of Pi = 3.141593  

Next TopicRange of Int in C

You may also like