Skip to content
StudyFame

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 %lf for double).

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, ...);
PartMeaning
"%d"The type of value to read
&variableThe address where the value is stored
Return valueHow 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

  1. int age;: a variable to store the input.
  2. printf("Enter your age: ");: a prompt that tells the user what to type. No \n, so the cursor waits on the same line.
  3. scanf("%d", &age);: waits for the user to type a number and press Enter, then stores it in age.
  4. The next two lines use the value like any other variable.

How scanf() Works

  1. When you type, the characters go into an input buffer (a waiting area). Nothing happens until you press Enter.
  2. scanf reads from the buffer and converts the text to the type in the format ("19" → the number 19).
  3. It stores the value at the address you gave (&age).
  4. For numbers, scanf skips spaces and Enter before the value. So you can type several numbers separated by spaces or on separate lines.
  5. 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 in stdio.h.
  • Use & before normal variables (not before string arrays).
  • Use %lf to read a double.
  • It returns the number of values read.
  • Always print a prompt before scanf so the user knows what to type.

Common Mistakes Beginners Make

MistakeWhat happensFix
scanf("%d", age); (no &)Crash or wrong valuescanf("%d", &age);
scanf("%d\n", &age);Waits for more input after EnterDon't put \n in scanf's format
scanf("%f", &d); with double dWrong valueUse %lf for double
scanf("Enter: %d", &n);Expects the user to type "Enter:"Put the prompt in printf, not scanf
Reading %c after %dThe char reads the leftover EnterUse " %c" (see the scanf problems lesson)

scanf() Practice Questions

  1. What is scanf()? What does it return?
  2. Why is & used in scanf?
  3. Why is & not used when reading a string with %s?
  4. Which format specifier reads a double?
  5. What does scanf return if the user types "abc" for %d?
Show answers
  1. A library function that reads formatted input from the keyboard. It returns the number of values successfully read.
  2. To give scanf the address of the variable, so it knows where to store the value.
  3. Because the name of an array already gives its address.
  4. %lf
  5. 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)

  1. scanf() is used for:

    A) Output   B) Input   C) File handling   D) Math

  2. The & in scanf("%d", &x) gives:

    A) The value of x   B) The address of x   C) The size of x   D) The type of x

  3. Which reads a double correctly?

    A) scanf("%d", &d);   B) scanf("%f", &d);   C) scanf("%lf", &d);   D) scanf("%lf", d);

  4. scanf("%d %d", &a, &b) returns 2 when:

    A) Both values are read   B) No value is read   C) Always   D) Never

  5. Which needs no &?

    A) int   B) float   C) char   D) A string (char array)

Show answers
  1. B: input.
  2. B: the address of x.
  3. C: scanf("%lf", &d);
  4. A: when both values are read.
  5. 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 formscan formatted
Header filestdio.h
TypeFormatted input function
Syntaxscanf("%d", &x);
&Address-of operator
ReturnsNumber 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. %s stops at a space. Use fgets() instead.

Remember

  • scanf needs the address (courier needs your home address): &x.
  • No \n inside 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.