Bytebook Compiler

Getting Started

User Input with scanf

Read numbers, characters and whole lines from the keyboard — and avoid the three classic scanf traps.

4 min read6 runnable examples4-question quiz

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 &:

C
#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;
}
Output
Enter your age: 21
In 5 years you will be 26.
Sample input is includedTry it Yourself

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:

C
#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;
}
Output
Enter two numbers: 12 7
Sum = 19, Difference = 5, Product = 84
Quotient = 1.71
Sample input is includedTry it Yourself

Reading decimals: %f versus %lf

printf prints both float and double with %f, but scanf is pickier:

Variable type scanf specifier
float %f
double %lf
C
#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;
}
Output
Weight in kg: 72.5
Height in m: 1.68
Your BMI is 25.7
Sample input is includedTry it Yourself

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:

C
#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;
}
Output
Quantity: 3
Confirm order? (Y/N): Y
You ordered 3 and answered Y
Sample input is includedTry it Yourself

Reading strings and whole lines

%s reads one word (it stops at a space). For a full line with spaces, use fgets:

C
#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;
}
Output
First name: Asha
City and state: Bengaluru, Karnataka
Asha lives in Bengaluru, Karnataka.
Sample input is includedTry it Yourself

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:

C
#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;
}
Output
Enter a number: abc
That was not a number!
Sample input is includedTry it Yourself

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    Which line correctly reads an integer into int n;?

  2. 2

    Which specifier should scanf use for a double?

  3. 3

    A user types Ravi Kumar for scanf("%s", name);. What ends up in name?

  4. 4

    Why does scanf(" %c", &ch); have a space before %c?