Node:Formatted input conversion specifiers, Previous:sscanf, Up:Formatted string input
Formatted input conversion specifiers
c
- Matches a fixed number of characters. If you specify a maximum field
width (see below), that is how many characters will be matched;
otherwise,
%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
- Matches an optionally signed decimal integer, containing the
following sequence:
- An optional plus or minus sign (
+
or-
). - One or more decimal digits.
Note that
%d
and%i
are not synonymous forscanf
, as they are forprintf
. - An optional plus or minus sign (
e
- Matches an optionally signed floating-point number, containing the
following sequence:
- An optional plus or minus sign (
+
or-
). - A floating-point number in decimal or hexadecimal format.
- The decimal format is a sequence of one or more decimal digits,
optionally containing a decimal point character (usually
.
), followed by an optional exponent part, consisting of a charactere
orE
, an optional plus or minus sign, and a sequence of decimal digits. - The hexadecimal format is a
0x
or0X
, 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 characterp
orP
, an optional plus or minus sign, and a sequence of digits.
- The decimal format is a sequence of one or more decimal digits,
optionally containing a decimal point character (usually
- An optional plus or minus sign (
E
- Same as
e
. f
- Same as
e
. g
- Same as
e
. G
- Same as
e
. i
- Matches an optionally signed integer, containing the following sequence:
- An optional plus or minus sign (
+
or-
). - A string of characters representing an unsigned integer.
- If the string begins with
0x
or0X
, the number is assumed to be in hexadecimal format, and the rest of the string must contain hexadecimal digits. - Otherwise, if the string begins with
0
, the number is assumed to be in octal format (base eight), and the rest of the string must contain octal digits. - Otherwise, the number is assumed to be in decimal format, and the rest of the string must contain decimal digits.
- If the string begins with
Note that
%d
and%i
are not synonymous forscanf
, as they are forprintf
. You can print integers in this syntax withprintf
by using the#
flag character with the%x
or%d
output conversions. (See printf.) - An optional plus or minus sign (
s
- Matches a string of non-whitespace characters. It skips initial
whitespace, but stops when it meets more whitespace after it has read
something. It stores a null character at the end of the text that it
reads, to mark the end of the string. (See String overflows with scanf, for a warning about using this conversion.)
x
- Matches an unsigned integer in hexadecimal format. The string matched
must begin with
0x
or0X
, and the rest of the string must contain hexadecimal digits. X
- Same as
x
. [
- Matches a string containing an arbitrary set of characters. For
example,
%12[0123456789]
means to read a string with a maximum field width of 12, containing characters from the set0123456789
-- 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.) %
- Matches a percent sign. Does not correspond to an argument, and does not permit flags, field width, or type modifier to be specified (see below).
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.)
- An optional
*
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. - An optional
a
flag, valid with string conversions only. This is a GNU extension toscanf
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.) - An optional
'
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 that1,000
will be read as one thousand. - An optional decimal integer that specifies the maximum field width.
The
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. - An optional type modifier character from the following table.
(The default type of the corresponding argument is
int *
for the%d
and%i
conversions,unsigned int *
for%x
and%X
, andfloat *
for%e
and its synonyms. You can use these type modifiers to specify otherwise.)h
- Specifies that the argument to which the value read should be assigned is
of type
short int *
orunsigned short int *
. Valid for the%d
and%i
conversions. l
- For the
%d
and%i
conversions, specifies that the argument to which the value read should be assigned is of typelong int *
orunsigned long int *
. For the%e
conversion and its synonyms, specifies that the argument is of typedouble *
. L
- For the
%d
and%i
conversions, specifies that the argument to which the value read should be assigned is of typelong long int *
orunsigned long long int *
. On systems that do not have extra-long integers, this has the same effect asl
.For the
%e
conversion and its synonyms, specifies that the argument is of typelong double *
. ll
- Same as
L
, for the%d
and%i
conversions. q
- Same as
L
, for the%d
and%i
conversions. z
- Specifies that the argument to which the value read should be assigned
is of type
size_t
. (Thesize_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.