Udemy logo

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:

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.

 

 

Page Last Updated: May 2014

Top courses in C#

Complete C# Masterclass
Denis Panjuta, Tutorials.eu by Denis Panjuta
4.6 (20,236)
Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (34,573)
Bestseller
Unity RPG Inventory Systems Asset Pack: Behind The Scenes
Sam Pattuzzi, GameDev.tv Team, Rick Davidson
4.6 (658)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.9 (42)
Bestseller
Complete C# Unity Game Developer 2D
GameDev.tv Team, Rick Davidson, Gary Pettie
4.7 (94,002)
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (2,797)
Bestseller
RPG Core Combat Creator: Learn Intermediate Unity C# Coding
Ben Tristem, Rick Davidson, Sam Pattuzzi, GameDev.tv Team
4.8 (9,877)
Design Patterns in C# and .NET
Dmitri Nesteruk
4.5 (9,238)
Bestseller
C# 10 | Ultimate Guide - Beginner to Advanced | Master class
Web University by Harsha Vardhan
4.5 (1,135)

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo