Node:Numeric Input Conversions, Next:, Previous:Table of Input Conversions, Up:Formatted Input



Numeric Input Conversions

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, 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 %o, %u, and %x conversions match unsigned integers in 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 (8, 10, or 16) for the base argument.

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 and %i conversions is int *, and unsigned int * for the other integer conversions. You can use the following type modifiers to specify other sizes of integer:

hh
Specifies that the argument is a signed char * or unsigned char *.

This modifier was introduced in ISO C99.

h
Specifies that the argument is a short int * or unsigned short int *.
j
Specifies that the argument is a intmax_t * or uintmax_t *.

This modifier was introduced in ISO C99.

l
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.

ll
L
q
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.

t
Specifies that the argument is a ptrdiff_t *.

This modifier was introduced in ISO C99.

z
Specifies that the argument is a size_t *.

This modifier was introduced in ISO C99.

All of the %e, %f, %g, %E, 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:

l
Specifies that the argument is of type double *.
L
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 General Numeric).

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.