Next: , Previous: , Up: Formatted Output   [Contents][Index]


12.12.4 Integer Conversions

This section describes the options for the ‘%d’, ‘%i’, ‘%b’, ‘%B’, ‘%o’, ‘%u’, ‘%x’, and ‘%X’ conversion specifications. These conversions print integers in various formats.

The ‘%d’ and ‘%i’ conversion specifications both print an int argument as a signed decimal number; while ‘%b’, ‘%o’, ‘%u’, and ‘%x’ print the argument as an unsigned binary, octal, decimal, or hexadecimal number (respectively). The ‘%X’ conversion specification is just like ‘%x’ except that it uses the characters ‘ABCDEF’ as digits instead of ‘abcdef’. The ‘%B’ conversion specification is just like ‘%b’ except that, with the ‘#’ flag, the output starts with ‘0B’ instead of ‘0b’.

The following flags are meaningful:

-

Left-justify the result in the field (instead of the normal right-justification).

+

For the signed ‘%d’ and ‘%i’ conversions, print a plus sign if the value is positive.

For the signed ‘%d’ and ‘%i’ conversions, if the result doesn’t start with a plus or minus sign, prefix it with a space character instead. Since the ‘+’ flag ensures that the result includes a sign, this flag is ignored if you supply both of them.

#

For the ‘%o’ conversion, this forces the leading digit to be ‘0’, as if by increasing the precision. For ‘%x’ or ‘%X’, this prefixes a leading ‘0x’ or ‘0X’ (respectively) to the result. For ‘%b’ or ‘%B’, this prefixes a leading ‘0b’ or ‘0B’ (respectively) to the result. This doesn’t do anything useful for the ‘%d’, ‘%i’, or ‘%u’ conversions. Using this flag produces output which can be parsed by the strtoul function (see Parsing of Integers) and scanf with the ‘%i’ conversion (see Numeric Input Conversions).

For the ‘%m’ conversion, print an error constant or decimal error number, instead of a (possibly translated) error message.

'

Separate the digits into groups as specified by the locale specified for the LC_NUMERIC category; see Generic Numeric Formatting Parameters. This flag is a GNU extension.

0

Pad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the ‘-’ flag is also specified, or if a precision is specified.

If a precision is supplied, it specifies the minimum number of digits to appear; leading zeros are produced if necessary. If you don’t specify a precision, the number is printed with as many digits as it needs. If you convert a value of zero with an explicit precision of zero, then no characters at all are produced.

Without a type modifier, the corresponding argument is treated as an int (for the signed conversions ‘%i’ and ‘%d’) or unsigned int (for the unsigned conversions ‘%b’, ‘%B’, ‘%o’, ‘%u’, ‘%x’, and ‘%X’). Recall that since printf and friends are variadic, any char and short arguments are automatically converted to int by the default argument promotions. For arguments of other integer types, you can use these modifiers:

hh

Specifies that the argument is a signed char or unsigned char, as appropriate. A char argument is converted to an int or unsigned int by the default argument promotions anyway, but the ‘hh’ modifier says to convert it back to a char again.

This modifier was introduced in ISO C99.

h

Specifies that the argument is a short int or unsigned short int, as appropriate. A short argument is converted to an int or unsigned int by the default argument promotions anyway, but the ‘h’ modifier says to convert it back to a short again.

j

Specifies that the argument is a intmax_t or uintmax_t, as appropriate.

This modifier was introduced in ISO C99.

l

Specifies that the argument is a long int or unsigned long int, as appropriate. Two ‘l’ characters are like the ‘L’ modifier, below.

If used with ‘%c’ or ‘%s’ the corresponding parameter is considered as a wide character or wide character string respectively. This use of ‘l’ was introduced in Amendment 1 to ISO C90.

L
ll
q

Specifies that the argument is a long long int. (This type is an extension supported by the GNU C compiler. On systems that don’t support 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.

wn

Specifies that the argument is a intn_t or int_leastn_t (which are the same type), for conversions taking signed integers, or uintn_t or uint_leastn_t (which are the same type), for conversions taking unsigned integers. If the type is narrower than int, the promoted argument is converted back to the specified type.

This modifier was introduced in ISO C2X.

wfn

Specifies that the argument is a int_fastn_t or uint_fastn_t, as appropriate. If the type is narrower than int, the promoted argument is converted back to the specified type.

This modifier was introduced in ISO C2X.

z
Z

Specifies that the argument is a size_t.

z’ was introduced in ISO C99. ‘Z’ is a GNU extension predating this addition and should not be used in new code.

Here is an example. Using the template string:

"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"

to print numbers using the different options for the ‘%d’ conversion gives results like:

|    0|0    |   +0|+0   |    0|00000|     |   00|0|
|    1|1    |   +1|+1   |    1|00001|    1|   01|1|
|   -1|-1   |   -1|-1   |   -1|-0001|   -1|  -01|-1|
|100000|100000|+100000|+100000| 100000|100000|100000|100000|100000|

In particular, notice what happens in the last case where the number is too large to fit in the minimum field width specified.

Here are some more examples showing how unsigned integers print under various format options, using the template string:

"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
|    0|    0|    0|    0|    0|    0|    0|  00000000|
|    1|    1|    1|    1|   01|  0x1|  0X1|0x00000001|
|100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|

Next: Floating-Point Conversions, Previous: Table of Output Conversions, Up: Formatted Output   [Contents][Index]