Getting Started
User Input with scanf
Read numbers, characters and whole lines from the keyboard — and avoid the three classic scanf traps.
scanf (scan formatted) reads input from the keyboard. It uses the same format specifiers as printf, but it needs the address of each variable, written with &:
#include <stdio.h>
int main(void) {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("In 5 years you will be %d.\n", age + 5);
return 0;
}Enter your age: 21
In 5 years you will be 26.The green underlined text in the output is what the user typed. Click Try it Yourself and change the input box to test other values.
Why the & ?
scanf has to put a value into your variable, so it needs to know where the variable lives in memory. &age means "the address of age". You will understand this fully in the Pointers lesson — for now, remember: numbers and characters need &, strings do not.
Reading several values
One scanf can read many values. The user can separate them with spaces or new lines:
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d, Difference = %d, Product = %d\n", a + b, a - b, a * b);
printf("Quotient = %.2f\n", (float)a / b);
return 0;
}Enter two numbers: 12 7
Sum = 19, Difference = 5, Product = 84
Quotient = 1.71Reading decimals: %f versus %lf
printf prints both float and double with %f, but scanf is pickier:
| Variable type | scanf specifier |
|---|---|
float |
%f |
double |
%lf |
#include <stdio.h>
int main(void) {
double weight, height;
printf("Weight in kg: ");
scanf("%lf", &weight);
printf("Height in m: ");
scanf("%lf", &height);
printf("Your BMI is %.1f\n", weight / (height * height));
return 0;
}Weight in kg: 72.5 Height in m: 1.68 Your BMI is 25.7
Reading a character
%c reads the very next character — including a leftover newline from the previous input. Put a space before it (" %c") to skip whitespace first:
#include <stdio.h>
int main(void) {
int qty;
char confirm;
printf("Quantity: ");
scanf("%d", &qty);
printf("Confirm order? (Y/N): ");
scanf(" %c", &confirm); // the space skips the newline left by the Enter key
printf("You ordered %d and answered %c\n", qty, confirm);
return 0;
}Quantity: 3 Confirm order? (Y/N): Y You ordered 3 and answered Y
Reading strings and whole lines
%s reads one word (it stops at a space). For a full line with spaces, use fgets:
#include <stdio.h>
#include <string.h>
int main(void) {
char first[20], city[50];
printf("First name: ");
scanf("%19s", first); // no & for arrays; 19 leaves room for '\0'
getchar(); // eat the newline left behind
printf("City and state: ");
fgets(city, sizeof city, stdin); // reads spaces too
city[strcspn(city, "\n")] = '\0'; // remove the trailing newline
printf("%s lives in %s.\n", first, city);
return 0;
}First name: Asha City and state: Bengaluru, Karnataka Asha lives in Bengaluru, Karnataka.
Checking that input worked
scanf returns how many values it successfully read. If the user types letters where a number is expected, it returns 0:
#include <stdio.h>
int main(void) {
int n;
printf("Enter a number: ");
if (scanf("%d", &n) == 1)
printf("You typed %d\n", n);
else
printf("That was not a number!\n");
return 0;
}Enter a number: abc
That was not a number!Test yourself
4 questions · pick an answer to check it instantly
- 1
Which line correctly reads an integer into
int n;?scanfneeds the format string first and then the address of the variable. - 2
Which specifier should
scanfuse for adouble?scanfneeds%lffordouble. Onlyprintfaccepts%ffor both. - 3
A user types
Ravi Kumarforscanf("%s", name);. What ends up inname?%sstops reading at the first space. Usefgetsto read the whole line. - 4
Why does
scanf(" %c", &ch);have a space before%c?Without the space,%cwould read the newline still waiting in the input buffer.