Go to the first, previous, next, last section, table of contents.
#include <stdio.h> int scanf(const char *format, ...);
This function scans formatted text from stdin
and stores it in
the variables pointed to by the arguments. See section scanf.
The format string contains regular characters which much match the input exactly as well as a conversion specifiers, which begin with a percent symbol. Any whitespace in the format string matches zero or more of any whitespace characters in the input. Thus, a single space may match a newline and two tabs in the input. All conversions except `c' and `[' also skip leading whitespace automatically. Each conversion specifier contains the following fields:
short
, `l' to specify doubles or long ints, or `L' or
`ll' (two lower-case ell letters) to specify long doubles and the
long long type. If the `h' qualifier appears before a specifier
that implies conversion to a long
or float
or
double
, like in `%hD' or `%hf', it is generally
ignored.
c
d
int
using 10 as the base of the
number representation.
hd
short
using 10 as the base.
ld
D
long
using 10 as the base.
Ld
lld
lD
long long
using 10 as the base.
e
E
f
F
g
G
float
).
le
lE
lf
lF
lg
lG
double
.
Le
LE
lle
llE
Lf
LF
llf
llF
Lg
LG
llg
llG
long double
.
i
0x
or 0
prefixes, and store in an int
.
See section strtol.
hi
short
.
li
I
long
.
Li
lli
lI
long long
.
n
int
pointed to by the argument.
hn
short
.
ln
long
.
Ln
lln
long long
.
o
int
, using base 8.
ho
short
, using base 8.
lo
O
long
, using base 8.
Lo
llo
lO
long long
, using base 8.
p
x
format.
s
u
int
using 10 as the base.
hu
short
using 10 as the base.
lu
U
long
using 10 as the base.
Lu
llu
lU
long long
using 10 as the base.
x
X
int
, using base 16.
hx
hX
short
, using base 16.
lx
lX
long
, using base 16.
Lx
LX
llx
llX
long long
, using base 16.
[...]
char
array, followed by a
terminating null character. If you do not specify the width
parameter, scanf
behaves as if width had a very large
value. Up to width characters are consumed and assigned, provided
that they match the specification inside the brackets. The characters
between the brackets determine which characters are allowed, and thus
when the copying stops. These characters may be regular characters
(example: `[abcd]') or a range of characters (example:
`[a-d]'). If the first character is a caret (`^'), then the
set specifies the set of characters that do not get copied (i.e. the
set is negated). To specify that the set contains a close-bracket
(`]'), put it immediately after `[' or `[^'. To specify
a literal dash (`-'), write it either immediately after `[' or
`[^', or immediately before the closing `]'.
%
Integer formats make use of strtol
or strtoul
to perform
the actual conversions. Floating-point conversions use strtod
and _strtold
.
The number of items successfully matched and assigned. If input ends,
or if there is any input failure before the first item is converted and
assigned, EOF
is returned. Note that literal characters
(including whitespace) in the format string which matched input
characters count as "converted items", so input failure after
such characters were read and matched will not cause EOF
to be returned.
ANSI (see note 1), POSIX
Notes:
U
are DJGPP extensions; they are provided for compatibility with Borland C and other compilers. The conversion specifiers for the long long
data type are GCC extensions. The meaning of `[a-c]' as a range of characters is a very popular extension to ANSI (which merely says a dash "may have a special meaning" in that context).
int x, y; char buf[100]; scanf("%d %d %s", &x, &y, buf); /* read to end-of-line */ scanf("%d %[^\n]\n", &x, buf); /* read letters only */ scanf("%[a-zA-Z]", buf);
Go to the first, previous, next, last section, table of contents.