Basics of the Scanf Function
The last chapter was about the printf() function, where we learned a lot about the functionality of this function. I have dedicated a couple of tutorials to the scanf()
function, too. In this tutorial, we will start with the basics of the scanf() function. So, let’s get into it.
Introduction to the scanf() Function
Just like the printf()
function, the scanf()
function is available in the stdio.h header file. The printf()
is dedicated to displaying some text on the screen (precisely formatted text), and the scanf()
allows accepting values from the user via the screen (or the console window).
The syntax of the scanf()
function looks very similar to the printf()
:
scanf(format-string, &var1, &var2, …);
Similar to the printf()
function, it accepts a format string with format specifiers (a.k.a. conversion specifications) and normal characters. The number of format specifiers must match the variables passed as the arguments.
You may have noticed that all variables are passed with the & symbol, which is the address-of operator. This is the major difference between the printf()
and the scanf()
functions—the printf()
function accepts variables without the & symbol.
Now that we have a basic understanding of the scanf()
function, let’s see how it actually works.
How Does scanf() Work?
To understand the working of the scanf()
function, let’s consider the following code snippet:
int var;
scanf("%d", &var);
The scanf()
function above has the capability to receive one value from the user and store it in the variable var
. The format specifier %d
tells the scanf()
function to receive an integer value from the user. The scanf()
function stores the value in the variable var
, which is the second argument (with the & symbol).
The complete program is as follows:
#include <stdio.h>
int main() {
int var;
scanf("%d", &var);
printf("%d", var); // to verify the value has been received.
return 0;
}
When you execute the above program, you will see the output screen with a blinking cursor demanding a value. Let’s say you passed 10. After hitting enter (or return), you will observe 10 displays on the screen that show our program is working as expected. You will observe the following (the first value is the user input):
10
10
It can be observed that the printf()
function is doing the opposite. It takes the value from a variable and displays that on the screen. The value of the variable var
, which is 10, is displayed on the screen. Also, we are not passing the ampersand (&) symbol along with the variable var
. The obvious question is, “What is the role of the ampersand (&) symbol in the scanf()
function, then?” Let’s understand this now.
The Address-of (&) Operator
The ampersand (&) symbol in C is also called the address-of operator. As the name suggests, this symbol can be used to get the address of a variable.
In the example discussed before, the address-of operator (&) was helping us get the address of the variable var
. This is precisely what the scanf()
function was demanding from us. To store the value received from the user, the address of a variable is needed. That is why &var
was passed, not the variable var
.
Why don’t we pass the address of a variable in the printf() function?
This is because the printf()
function needs the value to be displayed. Passing the name of a variable is the same as passing the value stored in it. In the case of the scanf()
function, we have to pass the address of the variable, as it needs to store the value received from the user in a memory location. Placing an ampersand (&) before the variable name allows us to obtain the address of that specific variable.
Summary
- The
scanf()
function helps accept input from the user.
- It stores the received input in a variable.
- To store the input, the address of the variable needs to be passed.
- The address-of operator (&) helps get the address of a variable.
- Place it before the variable to get the address.
- We pass the address of the variable because
scanf()
needs this address to store data received from the user.
Review Questions
Q1. To store user input in a variable using scanf(), we must pass the __________ of that variable to the function.
Answer: address
Q2. What will be the output of the following code if the user enters 25?
#include <stdio.h>
int main() {
int age;
scanf("%d", &age);
printf("You entered: %d", age + 5);
return 0;
}
Answer: You entered: 30.
Q3. Which of the following statements about the scanf() function is correct?
(a) scanf() requires the value of the variable.
(b) scanf() is used to display output on the console.
(c) scanf() requires the address of the variable to store user input.
(d) scanf() requires the address of the variable to store input.
Answer: Option (c) is the correct option. The scanf() function requires the address of the variable to store user input.
Q4. The format specifier used to read a floating-point number in scanf() is __________.
Answer: %f
Q5. Why can’t we just write scanf(”%d”, var);
instead of scanf("%d", &var);
for integer input?
Answer: This is because scanf() needs the address of a variable where it will store the user input. Without the ampersand, scanf() gets the value (not the location) and thus could not be able to store data in the variable.
Q6. What is wrong with the following code?
#include <stdio.h>
int main() {
int score;
scanf("%d", score);
printf("%d", score);
return 0;
}
Answer: The problem is with the scanf() function. The variable score has been passed as the second argument without the ampersand. This function does not work as intended.
Leave a comment