{ }codelper

Reference › C

C scanf(): Read Input

scanf reads values from the input (standard input) according to a format and stores them in variables. It needs to know where to store each value, not the variable's value, so you put & in front of the variable. You need #include <stdio.h>.

scanf("format", &var1, &var2, ...);

Reading integers and floating-point numbers

Read an int with %d and a double with %lf. printf prints a double with %f, but in scanf it must be %lf (%f is for float). For long long, use %lld.

#include <stdio.h> int main(void) {    int n;    double x;    scanf("%d", &n);    scanf("%lf", &x);    printf("%d\n", n * 2);    printf("%.2f\n", x / 2);    return 0;}
Input
21
3.5
Output
42
1.75

Edit and run it →

Reading several values at once

Write several format specifiers and pass the addresses of the variables in the same order. %d skips all spaces, tabs and newlines before the number. So the values are read the same way whether they are on one line or spread over several lines.

#include <stdio.h> int main(void) {    int a, b, c;    scanf("%d %d %d", &a, &b, &c);    printf("%d\n", a + b + c);    return 0;}
Input
3
   4 5
Output
12

Edit and run it →

One space in the format means skip any number of spaces (even none). Writing them together as "%d%d%d" gives the same result.

Reading one character: the space before %c

Unlike the other format specifiers, %c reads spaces and newlines as characters too. To read a visible character, put a space in front, as in " %c", so they are skipped first.

#include <stdio.h> int main(void) {    int a, b;    char op;    scanf("%d %c %d", &a, &op, &b);    if (op == '+') printf("%d\n", a + b);    if (op == '-') printf("%d\n", a - b);    return 0;}
Input
12 + 30
Output
42

Edit and run it →

The value scanf returns

scanf returns the number of values it successfully read and stored. If the input ends before it reads anything, it returns EOF (a negative number). With this you can read to the end of an input when you don't know how many numbers it has.

#include <stdio.h> int main(void) {    int x, count = 0, sum = 0;    while (scanf("%d", &x) == 1) {        count++;        sum += x;    }    printf("%d %d\n", count, sum);    return 0;}
Input
10 20
30
Output
3 60

Edit and run it →

Write the condition as == 1, not != EOF. If there are letters such as abc where a number should be, scanf returns 0 and leaves those letters in the input. A loop with != EOF keeps getting stuck on the same letters and never ends.

Common mistakes

Leaving out the & before the variable

int n;
scanf("%d", n);   /* should be &n */

→ This is undefined behavior. The garbage value that was in n is treated as an address and scanf tries to write there, so the program usually crashes

Pass the address, as in scanf("%d", &n);. The one exception is a char array that holds a string: its name is already an address, so you don't add &.

Reading a double with %f

double x;
scanf("%f", &x);   /* should be %lf */
printf("%f\n", x);

→ This is undefined behavior. It often prints a strange value that is different from the number you entered

In scanf, a double takes %lf and a float takes %f. printf prints both with %f, so they are easy to mix up.

Reading with %c after a number gives you the newline

#include <stdio.h>

int main(void) {
    int n;
    char c;
    scanf("%d", &n);
    scanf("%c", &c);
    printf("%d\n", c);
    return 0;
}

→ With the two input lines 5 and A, you get 10, the code of the newline, instead of 65, the code of A

%d reads only the number and leaves the newline after it. Put a space before %c to skip it, as in scanf(" %c", &c);.

Writing \n at the end of the format

int n;
scanf("%d\n", &n);   /* added \n like in printf */
printf("%d\n", n);

→ When you type a number on the keyboard and press Enter, it doesn't move on right away. It waits until the next character that is not a space arrives

A \n in the format doesn't mean one newline. It means read all the spaces and newlines that follow. Don't put anything at the end, as in scanf("%d", &n);.

Practice problems

See also