scanf() Function in C
So far, all values were written inside the program. Real programs ask the user for input: their name, age or marks.
In C, you read input from the keyboard with scanf(). In this lesson you will learn its syntax, why we write & before variables, and how to read many values at once.
What You Will Learn
- What
scanf()does. - The syntax of
scanf(). - Why the
&(address-of) operator is needed. - How to read one or many values.
- The return value of
scanf().
scanf() Function in C Explained
Exam definition: scanf() is a library function declared in stdio.h that reads formatted input from the keyboard and stores it in variables. It returns the number of values successfully read.
- The name means scan formatted.
- It is the opposite of
printf(): printf shows data, scanf takes data. - It uses the same format specifiers:
%d,%f,%c,%s... (with%lffordouble).
Why do we write & in scanf?
& is the address-of operator. &age means "the memory address of age". scanf needs to know where to store the value, so it needs the address, not the current value.
Memory trick: printf needs the value (to show it). scanf needs the address (to deliver it), like a courier needs your home address to deliver a parcel. & = address.
Syntax of scanf() in C
scanf("format specifiers", &variable1, &variable2, ...);
| Part | Meaning |
|---|---|
"%d" | The type of value to read |
&variable | The address where the value is stored |
| Return value | How many values were read successfully |
Exception: for a string (character array), don't use &: scanf("%s", name);, because an array name already gives its address.
Example 1: Read the User's Age
#include <stdio.h>
int main(void)
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old.\n", age);
printf("Next year you will be %d.\n", age + 1);
return 0;
}
Output
Enter your age: 19 You are 19 years old. Next year you will be 20.
Here 19 is what the user typed, followed by Enter.
Code Explanation
int age;: a variable to store the input.printf("Enter your age: ");: a prompt that tells the user what to type. No\n, so the cursor waits on the same line.scanf("%d", &age);: waits for the user to type a number and press Enter, then stores it inage.- The next two lines use the value like any other variable.
How scanf() Works
- When you type, the characters go into an input buffer (a waiting area). Nothing happens until you press Enter.
- scanf reads from the buffer and converts the text to the type in the format (
"19"→ the number 19). - It stores the value at the address you gave (
&age). - For numbers, scanf skips spaces and Enter before the value. So you can type several numbers separated by spaces or on separate lines.
- It returns how many values it stored. If the user types letters for
%d, it returns 0.
Example 2: Read Two Numbers and Print Their Sum
#include <stdio.h>
int main(void)
{
int a, b;
printf("Enter two numbers: ");
int count = scanf("%d %d", &a, &b);
printf("Values read: %d\n", count);
printf("Sum = %d\n", a + b);
return 0;
}
Output
Enter two numbers: 12 30 Values read: 2 Sum = 42
One scanf can read many values. The user can type them on one line with spaces, or press Enter after each one. count is 2 because two values were read.
Important Points to Remember
scanf()is declared instdio.h.- Use
&before normal variables (not before string arrays). - Use
%lfto read adouble. - It returns the number of values read.
- Always print a prompt before scanf so the user knows what to type.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
scanf("%d", age); (no &) | Crash or wrong value | scanf("%d", &age); |
scanf("%d\n", &age); | Waits for more input after Enter | Don't put \n in scanf's format |
scanf("%f", &d); with double d | Wrong value | Use %lf for double |
scanf("Enter: %d", &n); | Expects the user to type "Enter:" | Put the prompt in printf, not scanf |
Reading %c after %d | The char reads the leftover Enter | Use " %c" (see the scanf problems lesson) |
scanf() Practice Questions
- What is
scanf()? What does it return? - Why is
&used in scanf? - Why is
¬ used when reading a string with%s? - Which format specifier reads a
double? - What does scanf return if the user types "abc" for
%d?
Show answers
- A library function that reads formatted input from the keyboard. It returns the number of values successfully read.
- To give scanf the address of the variable, so it knows where to store the value.
- Because the name of an array already gives its address.
%lf- 0 (no value was read).
Coding Exercise
Write a program that asks for the length and width of a rectangle (as decimal numbers) and prints its area with 2 decimal places.
Show solution
#include <stdio.h>
int main(void)
{
float length, width;
printf("Enter length: ");
scanf("%f", &length);
printf("Enter width: ");
scanf("%f", &width);
printf("Area = %.2f\n", length * width);
return 0;
}
Enter length: 5.5 Enter width: 4 Area = 22.00
Quick Quiz: scanf() in C (MCQ)
-
scanf() is used for:
A) Output B) Input C) File handling D) Math
-
The
&inscanf("%d", &x)gives:A) The value of x B) The address of x C) The size of x D) The type of x
-
Which reads a
doublecorrectly?A)
scanf("%d", &d);B)scanf("%f", &d);C)scanf("%lf", &d);D)scanf("%lf", d); -
scanf("%d %d", &a, &b)returns 2 when:A) Both values are read B) No value is read C) Always D) Never
-
Which needs no
&?A)
intB)floatC)charD) A string (char array)
Show answers
- B: input.
- B: the address of x.
- C:
scanf("%lf", &d); - A: when both values are read.
- D: a string (char array).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Full form | scan formatted |
|---|---|
| Header file | stdio.h |
| Type | Formatted input function |
| Syntax | scanf("%d", &x); |
& | Address-of operator |
| Returns | Number of values read successfully |
| double | %lf |
Frequently asked questions
- Why is & used in scanf but not in printf?
- scanf must store a value, so it needs the address; printf only shows the value.
- What does scanf return?
- The number of values successfully read (EOF if input ends before anything is read).
- Why is & not used with %s?
- An array name already gives its address.
- Can scanf read a full name with spaces using %s?
- No.
%sstops at a space. Usefgets()instead.
Remember
- scanf needs the address (courier needs your home address): &x.
- No
\ninside scanf's format.
Next Topic
Next lesson: getchar() and putchar() in C. You will learn simple functions that read and print one character at a time.