πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Lesson 5: scanf β€” Read keyboard input

Read user input into variables with scanf. Learn why & matters, the %lf gotcha, and the typical bugs.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • scanf("%d", &x); reads into a variable
  • Always put & before the variable
  • double uses %lf (not %f!)
  • Don't put extra characters in the format string
⭐ Read if you have time
  • Check scanf's return value for errors
  • Whitespace skipping rules
  • Leftover-input problems and fixes
  • For strings, prefer fgets (safer)

scanf basics β€” the opposite of printf

Where printf "sends text to the screen", scanf "reads input from the keyboard". It's the mirror image.
πŸ“€ printf (output)
printf("%d\n", a);
// print variable a to screen
πŸ“₯ scanf (input)
scanf("%d", &a);
// read from keyboard into variable a

The standard pattern

int a;
printf("Enter a: ");   // prompt
scanf("%d", &a);       // read input
printf("a = %d\n", a);
Flow: β‘  prompt with printf β†’ β‘‘ read with scanf β†’ β‘’ use the value. This 3-step pattern is the bread and butter.

⚠️ Why & (ampersand) is required

The #1 rule of scanf: put & before every variable. Forgetting it typically crashes the program.
πŸ”΄ What happens if you forget &
int a;
scanf("%d", a);    // ❌ forgot &
Result:
β€’ The compiler emits a warning (take warnings seriously)
β€’ Running it almost always produces a segmentation fault (crash)
β€’ Worst case: writes into another memory region and causes a hard-to-find bug elsewhere

What & really is: the variable's "address"

The variable a holds a value. &a is "the memory address where a lives".
a = "contents of the box" (value)
&a = "location of the box" (address)

scanf needs to know where to write the value.
So we pass the address (&a).
πŸ’‘ Rule: always prefix scanf variables with &; never prefix printf's. (printf only reads values; it doesn't need an address.) The detailed mechanism is covered in Pointer Basics.

Exception: strings (arrays)

char name[50];
scanf("%s", name);   // ← no & for array name
Why: an array name is already an address (special rule). For now, just remember: "arrays are the exception".

🧩 The %lf mystery β€” scanning a double

In scanf, double uses %lf. Different from printf, and a classic source of confusion.
Typescanf specifierprintf specifier
int%d%d
float%f%f
double%lf ⚠️%f (%lf also OK)
char%c%c
string%s%s
πŸ”΄ Common mistake: %f for a double
double b;
scanf("%f", &b);    // ❌ double needs %lf
Result: b ends up with a completely wrong value (often 0.0 or huge). It's undefined behaviour. Compile with -Wall to get warnings.
Mnemonic: scanf + double β†’ use %lf (the l is for "long"). Only scanf is picky here.

scanf simulator

Type a value and press Enter to see how scanf would store it.
Enter a:
variable a
(empty)
printf("%d", a)
β€”

Common pitfalls

Pitfall β‘  missing &
scanf("%d", &a);
Omitting & usually crashes
Pitfall β‘‘ %f for double
scanf("%lf", &b);
double uses %lf in scanf
Pitfall β‘’ extra chars
scanf("%d\n", &a); ← no \n
scanf("Enter %d", &a); ← no prose
Pitfall β‘£ consecutive scanfs
Leftover newlines can bite when mixing %c with other specifiers.

βœ… Safe template

int a;
double b;

printf("Enter int: ");
scanf("%d", &a);

printf("Enter double: ");
scanf("%lf", &b);  // lf!

printf("a=%d, b=%f\n", a, b);

πŸ’‘ Bonus: check the return value

scanf returns "how many items it read successfully". Useful for detecting bad input.
int a;
if (scanf("%d", &a) != 1) {
    printf("Please enter an integer\n");
    return 1;
}
For production input, prefer fgets + sscanf. See Safe String Handling.

Try it yourself β€” scanf

The browser simulator substitutes a prompt for scanf input.
scanf_demo.c
Output
Press "Run" to execute...
πŸ’‘ Try these ideas
Advertisement

Related Lessons

Getting Started
printf
Printing values to the screen with format specifiers.
Advanced
Pointer Basics
What & really does: addresses and pointers.
Advanced topics
Safe String Handling
Safer alternatives: fgets + sscanf.
← Previous
printf
Next β†’
Quiz: Variables & printf