Reference: scanf
Function
This page is a part of the C programming language course I'm writing. If you want to learn C from the very beginning, start learning C here.
Basic usage
To read one integer from standard input, use
int variable;
scanf("%d", &variable); // pass an address!
Similar to printf
, a format specifier %d
tells scanf
that it needs to read a number, which will be placed in the specified location in memory pointed to by &variable
.
Wondering what &
means? Go read the page about pointers first, then come back here.
Reading multiple variables
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
This usage will assume that there are spaces or linebreaks between values, e.g. 1 2 42
. If there's something else, like slashes, you could let scanf
know that it must skip delimiters. Let's read a date:
int year, month, day;
scanf("%d/%d/%d", &year, &month, &day);
This will accept inputs like 2025/5/11
.
scanf
returns number of values read
Sometimes you don't know if your input has finished, or not. It is important to know that scanf
returns number of values successfully read. In the above example with the dates, if everything is fine, it will return 3. If not, it might return 0 if no values were read (maybe there was something but not a number), 1 if only one value was there, and so on.
The best way to use scanf
is to always compare it with the expected number of parameters you read.
if (scanf("%d/%d/%d", &year, &month, &day) != 3) {
printf("invalid input!\n");
}
Reading a string
scanf
can read words with %s
. It will stop reading when it finds a space or a line break character. Note that a string is an array of characters, and an array can be "decayed" to a pointer to its 0-th element, so you don't need to take an address when reading into a character array:
char str[100];
scanf("%s", str); // overflow is possible!
To prevent a possible overlow (reading beyond the boundary of your array), you can limit the number of characters that scanf
will read. Remember to leave one extra character to terminate your string with a zero!
char str[100];
scanf("%99s", str); // will not read more than 99 characters
If you want to read the whole line, not stopping at the first space character, use fgets
instead:
char str[100];
fgets(str, 100, stdin); // be careful: the last character might not be 0
Skip a value
Sometimes you want scanf
to read a value but you don't want to store it in a variable. Use %*d
(or %*s
, etc.) to skip one value:
int a, b;
scanf("%d %*d %d", &a, &b); // reads 3 numbers, but stores only two
Other formats
Here are format specifiers that you might need to use:
%d
: signed integers%u
: unsigned integers%f
:float
variables%lf
:double
variables%s
: strings%lld
:long long int
variables%llu
:unsigned long long int
variables
Playground
If you want to test how scanf
works, do it here!
#include <stdio.h>
int main() {
int year, month, day;
if (scanf("%d/%d/%d", &year, &month, &day) != 3) {
printf("invalid input!\n");
return 1; /* we return a non-zero exit code if there's an error */
}
printf("we read year: %d, month: %d, day: %d\n", year, month, day);
return 0;
}