This section describes the scanf
conversions for reading numeric
values.
The ‘%d’ conversion matches an optionally signed integer in decimal
radix. The syntax that is recognized is the same as that for the
strtol
function (see Parsing of Integers) with the value
10
for the base argument.
The ‘%i’ conversion matches an optionally signed integer in any of
the formats that the C language defines for specifying an integer
constant. The syntax that is recognized is the same as that for the
strtol
function (see Parsing of Integers) with the value
0
for the base argument. (You can print integers in this
syntax with printf
by using the ‘#’ flag character with the
‘%x’, ‘%o’, ‘%b’, or ‘%d’ conversion.
See Integer Conversions.)
For example, any of the strings ‘10’, ‘0xa’, or ‘012’
could be read in as integers under the ‘%i’ conversion. Each of
these specifies a number with decimal value 10
.
The ‘%b’, ‘%o’, ‘%u’, and ‘%x’ conversions match unsigned
integers in binary, octal, decimal, and hexadecimal radices, respectively. The
syntax that is recognized is the same as that for the strtoul
function (see Parsing of Integers) with the appropriate value
(2
, 8
, 10
, or 16
) for the base
argument. The ‘%b’ conversion accepts an optional leading
‘0b’ or ‘0B’ in all standards modes.
The ‘%X’ conversion is identical to the ‘%x’ conversion. They both permit either uppercase or lowercase letters to be used as digits.
The default type of the corresponding argument for the %d
,
%i
, and %n
conversions is int *
, and
unsigned int *
for the other integer conversions. You can use
the following type modifiers to specify other sizes of integer:
Specifies that the argument is a signed char *
or unsigned
char *
.
This modifier was introduced in ISO C99.
Specifies that the argument is a short int *
or unsigned
short int *
.
Specifies that the argument is a intmax_t *
or uintmax_t *
.
This modifier was introduced in ISO C99.
Specifies that the argument is a long int *
or unsigned
long int *
. Two ‘l’ characters is like the ‘L’ modifier, below.
If used with ‘%c’ or ‘%s’ the corresponding parameter is considered as a pointer to a wide character or wide character string respectively. This use of ‘l’ was introduced in Amendment 1 to ISO C90.
Specifies that the argument is a long long int *
or unsigned long long int *
. (The long long
type is an extension supported by the
GNU C compiler. For systems that don’t provide extra-long integers, this
is the same as long int
.)
The ‘q’ modifier is another name for the same thing, which comes
from 4.4 BSD; a long long int
is sometimes called a “quad”
int
.
Specifies that the argument is a ptrdiff_t *
.
This modifier was introduced in ISO C99.
Specifies that the argument is an intn_t *
or
int_leastn_t *
(which are the same type), or
uintn_t *
or uint_leastn_t *
(which are the
same type).
This modifier was introduced in ISO C23.
Specifies that the argument is an int_fastn_t *
or
uint_fastn_t *
.
This modifier was introduced in ISO C23.
Specifies that the argument is a size_t *
.
This modifier was introduced in ISO C99.
All of the ‘%e’, ‘%f’, ‘%g’, ‘%E’, ‘%F’ and ‘%G’
input conversions are interchangeable. They all match an optionally
signed floating point number, in the same syntax as for the
strtod
function (see Parsing of Floats).
For the floating-point input conversions, the default argument type is
float *
. (This is different from the corresponding output
conversions, where the default type is double
; remember that
float
arguments to printf
are converted to double
by the default argument promotions, but float *
arguments are
not promoted to double *
.) You can specify other sizes of float
using these type modifiers:
Specifies that the argument is of type double *
.
Specifies that the argument is of type long double *
.
For all the above number parsing formats there is an additional optional
flag ‘'’. When this flag is given the scanf
function
expects the number represented in the input string to be formatted
according to the grouping rules of the currently selected locale
(see Generic Numeric Formatting Parameters).
If the "C"
or "POSIX"
locale is selected there is no
difference. But for a locale which specifies values for the appropriate
fields in the locale the input must have the correct form in the input.
Otherwise the longest prefix with a correct form is processed.