Sprintf C : Understanding How the sprintf () Function Works in C

sprint cThe C programming language is one of the most powerful and efficient programming languages in the world. If you’re interested in learning programming or building applications, learning C should be the first step you take. Most of the modern-day programming languages have been inspired by it to some degree. C was developed back in the 1970s by Dennis M. Ritchie for a UNIX system. It is based on languages like B, CPL and BCPL. Unlike its predecessors, C is capable of both high level programming as well as low-level programming. C also does not take up much computer resources, so it can be used by small applications as well as large programs (like operating systems).  You can learn C with this beginners course.

In this tutorial, we’re going to take a look at the sprintf () library function in C. Library functions are built-in functions that can be used by referencing them at the beginning of the program. They contain code that can be reused in your program – they help you perform recurring tasks. Using library functions saves you time and makes your program more understandable.

If you’re new to C, you can learn more about the basics of the language by going through our tutorials. You can also sign up for this beginners course in C to if you want to learn the whole language, including the basics as well as the advanced concepts.

The sprintf () Function

The C library function sprintf () is used to store formatted data as a string. You can also say the sprintf () function is used to create strings as output using formatted data. The syntax of the sprintf () function is as follows:

int sprintf (char *string, const char *form, … );

Here, the *string will stand for the name of the array that will store the output obtained by working on the formatted data. The *form parameter will show the format of the output. Outputs can be obtained in different formats, like in the list below:

  • %d: You will obtain an integer output. For example: 5
  • %f: You will obtain a floating-point number in a fixed decimal form as your output. For example: 5.5.
  • %e: You will obtain a floating-point number that follows the exponential (scientific) format. For example: 5.050000e+01
  • %g: You will obtain a floating-point number that, depending on the size of the number, will be displayed in fixed decimal format or exponential format. No needless zeros will be displayed. For example: 5.05
  • %.1f: You will obtain a floating-point number with just a single digit displayed after the decimal point. For example: 5.5

You can also obtain other types of outputs by specifying different parameters for *form. For example, specifying “o” as the *form will get you an octal output. The other major outputs you can obtain include a hexadecimal output (signed and unsigned) as well as a string output.

The function sprintf () is included in the standard input output library stdio.h. The header has to be declared at the beginning of your program, like this:

#include <stdio.h>

Examples of the sprintf () Function

Let’s understand the sprintf () function better with the help of some examples. Here is a simple program that uses sprintf () to store the value of a variable in a character array:

#include <stdio.h>

int main()

{

char *stringa[30];

int *fingers = 5;

sprintf(stringa, "Number of fingers making up a hand are %f", fingers);

puts(stringa);

return(0);

}

Output:

The output of this program is: Number of fingers making up a hand are 5

As you can see, we’ve used the sprintf () function to put the value of an integer variable into a character array. To learn more about arrays, strings and the other basics of the language, take a look at the other Udemy posts on the topic. You can also take this course to learn to write your own C programs.

With sprintf (), you can combine several data variables into a character array. In the program above, we just used a single integer variable and stored it into an array. Let’s write a program that stores multiple bits of data into the array and then see the output:

#include <stdio.h>

int main()

{

char *stringa[50];

char *one = “We”;

char *two = “Are”;

char *three = “Creating”;

char *four = “a”;

char *five = “String”;

sprintf(stringa, "%s %s %s %s %s", one, two, three, four, five);

puts(stringa);

return(0);

}

Output:

We Are Creating a String

Here, we have used the sprintf () function to create a string using bits of formatted data (character variables). We combined 5 different character variables into a single variable using the sprintf () function.

The sscanf () Function vs the sprintf() Function

The sprintf () function is the opposite of the sscanf () function. The sscanf () function is used to extract bits of data from a character array and store them in a separate variable. For example, take a look at the following program:

#include <stdio.h>

int main()

{

char *stringa = “We Are Dismantling a String”;

char *one, *two, *three, *four, *five;

sscanf(stringa, “%s %s %s %s %s”, one, two, three, four, five);

return(0);

}

In this program, sscanf () will dismantle the string “We Are Dismantling a String” into five separate parts. The first word will be stored in char array one, the second in two, third in three, fourth in four and the fifth in five.

The sscanf () function and the sprintf () function are like the two sides of a coin. You can now use the sprintf() function to reassemble the string. You can use the same char array stringa- its previous value gets overwritten. Try it out for yourself to get a better grasp on it.

Library functions have been written by developers to make your life easier. It’s a good idea to learn as much about library functions as possible. You can take a look at your tutorials to learn other library functions or take this course to learn C programming language in its entirety.