Formatted Vs Unformatted Input/Output
This is the beginning of chapter 3—the printf() function. It is one of the output functions and also the most used one in C programming. Hence, I have dedicated a separate chapter to understanding this function in depth. In this tutorial, we start with the basics by learning the distinction between formatted and unformatted I/O. So, without any further ado, let’s begin.
Formatted Input/Output
In this tutorial, I will frequently use the words variables and arguments. Although we will dive deeper into these concepts later, we will have a basic understanding of them for now. A variable represents a location in memory. So, think of it as a container to hold a value for when we need it. On the other hand, an argument is the input to a function, enabling it to produce the desired result.
To enhance the readability and appearance, the formatted text in English uses styles like bold, italics, and underlining. On the other hand, unformatted text is merely text with no formatting. To further grasp the distinction between the two, consider the following examples:
“Hello! My name is Google, and I am a search engine. I can help you with the topic you want to search for.”
The text above is the plain text with no formatting. This is what the formatted version looks like:
“Hello! My name is Google, and I am a search engine. I can help you with the topic you want to search for.”
In the context of C programming, the major part of formatting is about embedding variables or external values in plain text with the help of special characters called format specifiers. A format specifier is nothing more than a placeholder for a variable in a string. We will come back to the topic shortly for complete understanding.
As we proceed in this course, we will get to know that formatting in C is not just about embedding external data in a text but also about alignment, width, precision, escape sequences, and many more.
When we read and write a formatted text through a function, we call it formatted input and output. This operation is usually done with the help of the scanf()
and printf()
functions, respectively.
The scanf()
function uses formatted text as its first argument to read values from the user. The formatted text (often called the format string) uses format specifiers for this purpose.
For example, in the following code, we are trying to read two integers from the user with the help of the scanf()
function:
scanf("%d%d", &a, &b);
The first argument to the scanf()
function is the format string "%d%d”
, in which we used %d
as the format specifier. It acts as the placeholder for an integer that we want to receive from the user. There are two of them, so clearly we are expecting two integers. In the background, the scanf()
receives integers and puts them in the same order inside variables a and b. Ultimately, variables a
and b
will store the integers.
Notice that the variables as arguments to the scanf() function are passed with &, which is called the address-of operator. This means, in our example, we are passing the addresses of variables a and b, and this method allows the scanf() function to store inputs in the memory locations corresponding to the addresses provided. We study this operator properly in a later chapter.
We have a dedicated chapter on the scanf() function in C, where we will study it in depth. For now, a surface-level understanding of the topic is more than enough.
In the same way, the printf()
function can be used to display text in a specific format through format specifiers.
Consider the following code snippet:
printf("I am %d years old.", age);
Like the scanf()
function, the first argument to the printf()
function is the format string "I am %d years old."
The %d
is used as the placeholder for the second argument provided to the printf()
function, which is the variable age
. At the time of execution, %d
will be replaced by the value of the age
variable.
In this chapter, we will dig into the depths of the printf() function. Just be patient! All your doubts will be clear after the chapter is completed.
Unformatted Input/Output
As the name suggests, unformatted input/output refers to the input and output obtained without any formatting. This approach is particularly useful when we don’t want any type of formatting in the input and the output.
We have functions that can take input and produce output that is not formatted. Some of these functions are getchar()
and putchar()
.
The getchar()
function is unformatted, and it takes a character from the user. We can use this function as is because it does not need any arguments. It returns the character received, which can then be stored in a variable as follows:
c = getchar();
The variable c
will hold the character from the user after getchar()
is executed.
Similarly, we have the putchar()
function, which is an unformatted output function that takes a character as the argument and prints it on the screen. The following code snippet allows printing the character 'A’
on the screen:
putchar('A');
The above functions are not using any formatting whatsoever. That’s why they are called unformatted input/output functions.
Summary
- Formatted input and output refers to the input and output obtained after doing some formatting.
- Unformatted input/output is obtained without any formatting.
- The functions
scanf()
and printf()
are examples of formatted input and output, respectively.
- The functions
getchar()
and putchar()
serve as examples of unformatted input and output, respectively.
Review Questions
Q1. Why is the printf()
function considered a “formatted output” function?
A. The printf()
function is considered a formatted output function because it allows embedding variables’ values in a string through format specifiers.
Q2. What would be the output of the following code?
int num = 10;
printf("The number is %d.", num);
A. Output: The number is 10.
Q3. How does the scanf()
function determine where to store the input value? How is this process different from how printf()
uses its arguments?
A. To the scanf()
function, we always need to pass the addresses of the variables where we want to store the inputs. As the addresses are passed, scanf()
knows in which memory locations the values need to be stored. This is not the case with the printf()
function. The printf()
function has to display the values stored in variables, so passing the names of variables is enough, as this will give the values stored in those variables.
Q4. How can printf()
be used to display multiple values in one line? Give an example.
A. You can use multiple format specifiers in the printf()
function to display multiple values in a single line. As an example, consider the following line of code:
int a = 10, b = 20; // variables a and b with values 10 and 20
printf("The value of a is %d and that of b is %d.", a, b);
In the above example, I have used two %d
s to print the values of both a
and b
in the same line.
Output: The value of a is 10, and that of b is 20.
Q5. True or False: The function putchar()
can be used to display multiple characters at once.
A. False. A single putchar()
function can only be used to print one character at a time.
Leave a comment