#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:
*
) which indicates that the input should be
converted according to the conversion spec, but not stored anywhere.
h
to specify
short
, l
to specify doubles or long ints or, or L
to specify long doubles. Long long type can be specified by L
or ll
.
c
d
e
E
f
g
G
i
0x
or 0
prefixes. See section strtol.
n
o
p
x
format.
s
NULL
-terminated.
u
x
X
[...]
c
format, except only certain characters are copied.
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 (]
), list that as the first regular character.
%
Most conversions make use of strtol
or strtoul
to perform
the actual conversions.
The number of items successfully matched and assigned. If input ends before first item is assigned, EOF is returned.
ANSI, POSIX
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.