The Printf Function
In the last tutorial, we understood the difference between formatted and unformatted input/output. We learned about the getchar() and putchar() functions, which we can use to get and put a character without any formatting. These were unformatted input and output functions. We also have formatted input/output functions such as scanf() and printf(). The goal of this tutorial is to make you understand the most frequently used formatted output function—the printf() function. So, let’s embark on this journey together.
Understanding the printf Function
The printf()
function is used to print or display something on the screen—it’s that simple! We need to supply some inputs to this function; behind the scenes it will act on those inputs, and as a result it will display what we wanted it to display. We don’t have to worry about the internal processing of the inputs; our job is to provide the inputs that help in displaying content that we want to display.
For Code::Blocks users:
By screen, I mean the console window that pops up when we click on the build and run button.
The following is the syntax of the printf()
function:
int printf(format-string, exp1, exp2, ...);
Let’s decode the syntax:
- The
int
that appears before printf()
indicates the return type of the printf()
function. It tells what type of value it returns. The printf()
function returns the number of characters it displays on the screen.
- The
format-string
refers to the sequence of characters that the printf()
function displays. It is mandatory to supply this input.
exp1, exp2, …
are expressions provided as inputs that, when evaluated, produce values that can be embedded in the appropriate places in the format string as specified
With examples, the syntax will be more clear. Consider the following call to the printf()
function:
printf("Hi! Nice to meet you.")
The above call to the printf()
function will display “Hi! Nice to meet you” on the screen. As mentioned, the format string is the sequence of characters displayed by the printf()
function. Here, the format string is “Hi! Nice to meet you.”
Notice that while calling the function, the return type of the printf()
function (which is int
) is not specified. A return type is needed when we define the function. The printf()
function is already defined; we are just calling it for execution.
Another example:
int age = 65; // age is an integer variable and its value is 10.
printf("My age is %d years.", age); // %d is the placeholder for variable "age"
This time we have passed two inputs to the printf()
function. The first is the format string, and the second is the variable age
. A variable is a named location in memory, and hence it is capable of holding some value in it. Precisely, age
is an integer variable (as int is specified before age); therefore, it can hold an integer value. In our example, the variable age
is holding the value 65—representing the age of some person.
Now, let’s return to the inputs of the printf()
function; this time the format string is slightly different. “My age is %d
years” is not making much sense. I mean, which person in the world has %d
age? In fact, %d
is a placeholder for an integer value from the second input passed to the printf()
function. In the background, the printf()
function will replace %d
by the value of the variable age. So, we will see the following output:
My age is 65 years.
Note to readers:
This is one of the main reasons why the printf()
function is so useful. We can embed values of the variables in a string, and there is virtually no limit on the number of values.
The %d
is not the only placeholder available to us; we have others too. We call these placeholders “conversion specifications,” but why this name? The upcoming topic will clarify this.
Conversion Specification a.k.a. Format Specifier
Inside the memory of the computer, every type of value is stored in the binary format, i.e., in the form of 0s and 1s. A variable represents a specific location in memory (precisely, the Random Access Memory, or RAM) where we can store some data. In the example we discussed before, we defined the variable age as follows:
int age = 65;
This variable age
represents a location in memory, and the value 65, which is assigned to the variable, is stored in memory in the binary format. To be precise, an integer variable takes 4 bytes of memory, which is equivalent to 32 bits, so the 32-bit equivalent of the number 65, which is 00000000 00000000 00000000 01000001
, will be stored.
Now, let’s say we want to print the value 65 on the screen. For this we can use the printf()
function, just like we did before:
printf("My age is %d years.", age);
We have passed the variable age as the second argument to the printf()
function, and this syntax is equivalent to passing the value stored in the memory location to which the variable is referring. But the value stored is just a sequence of 0s and 1s—how does the printf()
function know that it has to treat this binary number as an integer or a character or a fraction? How does printf()
make sense out of the cryptic sequence of 0s and 1s? The answer is the conversion specification.
As the name suggests, conversion specification tells printf()
to convert a binary number to a specific format. %d
is the conversion specification that tells the printf()
function to treat the binary number 00000000 00000000 00000000 01000001
as decimal (d in %d
stands for decimal). Therefore, printf()
will display 65 in place of %d
. If we update the line of code as follows, then the binary number will be treated as a character:
printf("My age is %c years.", age);
The equivalent character for the binary number is A, and hence we will see this result as the output in place of %c
.
Later in this tutorial series, we will discuss the encoding schemes for characters in C in depth.
The conversion specification is also called the format specifier because of the same reason—it helps specify the format in which the value should be displayed.
Note to the readers:
We will discuss different conversion specifications in the future tutorials, but essentially they do not provide the only way to format strings. We will explore more ways later in this series.
Summary
- We use the
printf()
function to display text with various formatting options.
- Conversion specifications and format specifiers are the names given to placeholders like %d, %c, %f, etc.
- Use %d to embed the value of an integer variable if the goal is to display it as an integer. %c for character and %f for fractions.
Review Questions
Q1. What is a function?
Ans. A function represents a block of code that can perform a specific task and can even return the result of the task performed.
Q2. What is f in the printf() function?
Ans. The f stands for “formatted.”
Q3. What type of function is printf()—formatted or unformatted?
Ans. Formatted output function.
Q4. Where does the variable’s data get stored?
Ans. Random Access Memory (RAM).
Q5. What is the meaning of conversion specification?
Ans. Conversion specification helps printf() decide the format of the binary data stored in memory. For example, %d tells printf() to treat the binary data as decimal.
Leave a comment