C Reference ยท stdio.h
C scanf() function
Reference for C scanf(): read formatted input from stdin, conversions, field widths, whitespace rules, the return value and how to check it safely.
scanf() reads characters from standard input, interprets them according to a format string, and stores the converted values through the pointers you pass. It is the input counterpart of printf(), but with an important difference: you pass addresses (&count), not values.
The return value is the key to using it correctly: it is the number of items successfully assigned, or EOF if the input ended before the first conversion. A program that ignores it is working with uninitialized variables whenever the input is bad. For line-oriented input, fgets() followed by sscanf() is usually safer; see scanf pitfalls.
#include <stdio.h> int scanf(const char *restrict format, ...);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int scanf(const char *restrict format, ...);
| Name | Description |
|---|---|
format |
Controls parsing. Whitespace matches any amount of whitespace (including none); ordinary characters must match exactly; % starts a conversion. |
... |
Pointers to the objects that receive the values, one per conversion (except %% and %*d-style suppressed ones). |
Common conversions:
| Specifier | Stores into | Notes |
|---|---|---|
%d / %i |
int * |
%i also accepts 0x hex and leading-0 octal |
%u, %x, %o |
unsigned * |
|
%ld, %lld, %hd, %zu |
long *, long long *, short *, size_t * |
length modifiers as for printf |
%f, %e, %g |
float * |
%lf for double *, %Lf for long double * |
%c |
char * |
Reads exactly the width (default 1) characters, does not skip whitespace, no terminator is added |
%s |
char * |
Skips whitespace, reads a run of non-whitespace, adds '\0' |
%[set] |
char * |
Reads characters from the set (%[a-z]) or not in it (%[^\n]) |
%n |
int * |
Stores the number of characters consumed so far; not counted in the result |
%*d |
none | Parse and discard (assignment suppression) |
%% |
none | Matches a literal % |
A maximum field width, as in %15s or %9[^,], limits how many characters are read. It does not include the terminating null, so the array must be one larger.
Returns: the number of input items assigned (may be fewer than requested, even 0), or EOF if input failed before the first conversion could finish.
Errors / undefined behavior: a pointer of the wrong type, too few pointers, or an array too small for %s or %[ without a width is undefined behavior. Overflow of a numeric conversion's result type is also undefined.
Usage notes
- Check the result against the number of conversions you asked for.
if (scanf("%d %d", &a, &b) != 2) { ... }. Variables not assigned remain uninitialized. - Use a width with
%sand%[.scanf("%s", buf)has no length limit and is an overflow bug; writescanf("%63s", buf)for a 64-byte array. - Whitespace handling. Most conversions skip leading whitespace, but
%c,%[and%ndo not. To skip whitespace before%c, put a space in the format:scanf(" %c", &ch). - A newline stays in the buffer. After
scanf("%d", &n)the Enter key's'\n'is unread, which breaks a followinggetchar()orfgets(). Read the rest of the line first, or read lines withfgetsfrom the start. - A failed conversion does not consume the offending character. If
%dmeetsabc, it assigns nothing and leavesabcunread; looping onscanf("%d", ...)then spins forever unless you discard the bad input. %fstores afloat. Reading into adoubleneeds%lf; this differs fromprintf, where both use%f. A mismatch is undefined behavior.%cand%scombined with%[do not need&for arrays; scalars (int,double,char) do.- Stdin may be buffered. With a terminal, input is delivered line by line, so a program does not see a keystroke until Enter.
- Locale affects the decimal point accepted by
%f. - Complexity: linear in the characters consumed.
- Alternatives:
fgets()andstrtol()give precise error handling;sscanf()parses a string you already have.