Node:Formatted input conversion specifiers, Previous:sscanf, Up:Formatted string input
c
%c
matches one character. This conversion does not
append a null character to the end of the text it reads, as does the
%s
conversion. It also does not skip whitespace characters, but
reads precisely the number of characters it was told to, or generates a
matching error if it cannot.
d
+
or -
).
Note that %d
and %i
are not synonymous for scanf
,
as they are for printf
.
e
+
or -
).
.
),
followed by an optional exponent part, consisting of a character
e
or E
, an optional plus or minus sign, and a sequence of
decimal digits.
0x
or 0X
, followed by a
sequence of one or more hexadecimal digits, optionally containing a
decimal point character, followed by an optional binary-exponent part,
consisting of a character p
or P
, an optional plus or
minus sign, and a sequence of digits.
E
e
.
f
e
.
g
e
.
G
e
.
i
+
or -
).
0x
or 0X
, the number is
assumed to be in hexadecimal format, and the rest of the string must contain
hexadecimal digits.
0
, the number is
assumed to be in octal format (base eight), and the rest of the
string must contain octal digits.
Note that %d
and %i
are not synonymous for scanf
,
as they are for printf
. You can print integers in this syntax
with printf
by using the #
flag character with the %x
or %d
output conversions. (See printf.)
s
x
0x
or 0X
, and the rest of the string must
contain hexadecimal digits.
X
x
.
[
%12[0123456789]
means to read a string with a maximum
field width of 12, containing characters from the set 0123456789
-- in other words, twelve decimal digits. An embedded -
character means a range of characters; thus %12[0-9]
means the
same thing as the last example. Preceding the characters in the square
brackets with a caret (^
) means to read a string not
containing the characters listed. Thus, %12[^0-9]
means to read
a twelve-character string not containing any decimal digit.
(See String overflows with scanf, for a warning about using this
conversion.)
%
In between the percent sign (%
) and the input conversion
character, you can place some combination of the following modifiers, in
sequence. (Note that the percent sign conversion (%%
) doesn't
use arguments or modifiers.)
*
flag. This flag specifies that a match should be made
between the conversion specifier and an item in the input stream, but
that the value should not then be assigned to an argument.
a
flag, valid with string conversions only.
This is a GNU extension to scanf
that requests allocation of a
buffer long enough to safely store the string that was
read. (See String overflows with scanf, for information on how to use
this flag.)
'
flag. This flag specifies that the number read will be grouped
according to the rules currently specified on your system. For example,
in the United States, this usually means that 1,000
will be read
as one thousand.
scanf
function will stop reading characters from the input stream
either when this maximum is reached, or when a non-matching character is read,
whichever comes first. Discarded initial whitespace does not count toward
this width; neither does the null character stored by string input conversions
to mark the end of the string.
int *
for the
%d
and %i
conversions, unsigned int *
for %x
and %X
, and float *
for %e
and its synonyms. You can use these
type modifiers to specify otherwise.)
h
short int *
or unsigned short int *
. Valid for the
%d
and %i
conversions.
l
%d
and %i
conversions, specifies that the argument
to which the value read should be assigned is of type long int *
or unsigned long int *
. For the %e
conversion and its
synonyms, specifies that the argument is of type double *
.
L
%d
and %i
conversions, specifies that the argument
to which the value read should be assigned is of type long long int *
or unsigned long long int *
. On systems that do not have
extra-long integers, this has the same effect as l
.
For the %e
conversion and
its synonyms, specifies that the argument is of type long double
*
.
ll
L
, for the %d
and %i
conversions.
q
L
, for the %d
and %i
conversions.
z
size_t
. (The size_t
type is used to specify
the sizes of blocks of memory, and many functions in this chapter use
it.) Valid for the %d
and %i
conversions.