Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdio.h>

int scanf(const char *restrict format, ...);

Try it

C Reading numbers and a word

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.

C Looping until the input ends or goes wrong

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

C
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 %s and %[. scanf("%s", buf) has no length limit and is an overflow bug; write scanf("%63s", buf) for a 64-byte array.
  • Whitespace handling. Most conversions skip leading whitespace, but %c, %[ and %n do 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 following getchar() or fgets(). Read the rest of the line first, or read lines with fgets from the start.
  • A failed conversion does not consume the offending character. If %d meets abc, it assigns nothing and leaves abc unread; looping on scanf("%d", ...) then spins forever unless you discard the bad input.
  • %f stores a float. Reading into a double needs %lf; this differs from printf, where both use %f. A mismatch is undefined behavior.
  • %c and %s combined 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() and strtol() give precise error handling; sscanf() parses a string you already have.