| <html lang="en"> |
| <head> |
| <title>Integer Conversions - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Formatted-Output.html#Formatted-Output" title="Formatted Output"> |
| <link rel="prev" href="Table-of-Output-Conversions.html#Table-of-Output-Conversions" title="Table of Output Conversions"> |
| <link rel="next" href="Floating_002dPoint-Conversions.html#Floating_002dPoint-Conversions" title="Floating-Point Conversions"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Integer-Conversions"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Floating_002dPoint-Conversions.html#Floating_002dPoint-Conversions">Floating-Point Conversions</a>, |
| Previous: <a rel="previous" accesskey="p" href="Table-of-Output-Conversions.html#Table-of-Output-Conversions">Table of Output Conversions</a>, |
| Up: <a rel="up" accesskey="u" href="Formatted-Output.html#Formatted-Output">Formatted Output</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">12.12.4 Integer Conversions</h4> |
| |
| <p>This section describes the options for the ‘<samp><span class="samp">%d</span></samp>’, ‘<samp><span class="samp">%i</span></samp>’, |
| ‘<samp><span class="samp">%o</span></samp>’, ‘<samp><span class="samp">%u</span></samp>’, ‘<samp><span class="samp">%x</span></samp>’, and ‘<samp><span class="samp">%X</span></samp>’ conversion |
| specifications. These conversions print integers in various formats. |
| |
| <p>The ‘<samp><span class="samp">%d</span></samp>’ and ‘<samp><span class="samp">%i</span></samp>’ conversion specifications both print an |
| <code>int</code> argument as a signed decimal number; while ‘<samp><span class="samp">%o</span></samp>’, |
| ‘<samp><span class="samp">%u</span></samp>’, and ‘<samp><span class="samp">%x</span></samp>’ print the argument as an unsigned octal, |
| decimal, or hexadecimal number (respectively). The ‘<samp><span class="samp">%X</span></samp>’ conversion |
| specification is just like ‘<samp><span class="samp">%x</span></samp>’ except that it uses the characters |
| ‘<samp><span class="samp">ABCDEF</span></samp>’ as digits instead of ‘<samp><span class="samp">abcdef</span></samp>’. |
| |
| <p>The following flags are meaningful: |
| |
| <dl> |
| <dt>‘<samp><span class="samp">-</span></samp>’<dd>Left-justify the result in the field (instead of the normal |
| right-justification). |
| |
| <br><dt>‘<samp><span class="samp">+</span></samp>’<dd>For the signed ‘<samp><span class="samp">%d</span></samp>’ and ‘<samp><span class="samp">%i</span></samp>’ conversions, print a |
| plus sign if the value is positive. |
| |
| <br><dt>‘<samp> </samp>’<dd>For the signed ‘<samp><span class="samp">%d</span></samp>’ and ‘<samp><span class="samp">%i</span></samp>’ conversions, if the result |
| doesn't start with a plus or minus sign, prefix it with a space |
| character instead. Since the ‘<samp><span class="samp">+</span></samp>’ flag ensures that the result |
| includes a sign, this flag is ignored if you supply both of them. |
| |
| <br><dt>‘<samp><span class="samp">#</span></samp>’<dd>For the ‘<samp><span class="samp">%o</span></samp>’ conversion, this forces the leading digit to be |
| ‘<samp><span class="samp">0</span></samp>’, as if by increasing the precision. For ‘<samp><span class="samp">%x</span></samp>’ or |
| ‘<samp><span class="samp">%X</span></samp>’, this prefixes a leading ‘<samp><span class="samp">0x</span></samp>’ or ‘<samp><span class="samp">0X</span></samp>’ (respectively) |
| to the result. This doesn't do anything useful for the ‘<samp><span class="samp">%d</span></samp>’, |
| ‘<samp><span class="samp">%i</span></samp>’, or ‘<samp><span class="samp">%u</span></samp>’ conversions. Using this flag produces output |
| which can be parsed by the <code>strtoul</code> function (see <a href="Parsing-of-Integers.html#Parsing-of-Integers">Parsing of Integers</a>) and <code>scanf</code> with the ‘<samp><span class="samp">%i</span></samp>’ conversion |
| (see <a href="Numeric-Input-Conversions.html#Numeric-Input-Conversions">Numeric Input Conversions</a>). |
| |
| <br><dt>‘<samp><span class="samp">'</span></samp>’<dd>Separate the digits into groups as specified by the locale specified for |
| the <code>LC_NUMERIC</code> category; see <a href="General-Numeric.html#General-Numeric">General Numeric</a>. This flag is a |
| GNU extension. |
| |
| <br><dt>‘<samp><span class="samp">0</span></samp>’<dd>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 ‘<samp><span class="samp">-</span></samp>’ |
| flag is also specified, or if a precision is specified. |
| </dl> |
| |
| <p>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. |
| |
| <p>Without a type modifier, the corresponding argument is treated as an |
| <code>int</code> (for the signed conversions ‘<samp><span class="samp">%i</span></samp>’ and ‘<samp><span class="samp">%d</span></samp>’) or |
| <code>unsigned int</code> (for the unsigned conversions ‘<samp><span class="samp">%o</span></samp>’, ‘<samp><span class="samp">%u</span></samp>’, |
| ‘<samp><span class="samp">%x</span></samp>’, and ‘<samp><span class="samp">%X</span></samp>’). Recall that since <code>printf</code> and friends |
| are variadic, any <code>char</code> and <code>short</code> arguments are |
| automatically converted to <code>int</code> by the default argument |
| promotions. For arguments of other integer types, you can use these |
| modifiers: |
| |
| <dl> |
| <dt>‘<samp><span class="samp">hh</span></samp>’<dd>Specifies that the argument is a <code>signed char</code> or <code>unsigned |
| char</code>, as appropriate. A <code>char</code> argument is converted to an |
| <code>int</code> or <code>unsigned int</code> by the default argument promotions |
| anyway, but the ‘<samp><span class="samp">h</span></samp>’ modifier says to convert it back to a |
| <code>char</code> again. |
| |
| <p>This modifier was introduced in ISO C99<!-- /@w -->. |
| |
| <br><dt>‘<samp><span class="samp">h</span></samp>’<dd>Specifies that the argument is a <code>short int</code> or <code>unsigned |
| short int</code>, as appropriate. A <code>short</code> argument is converted to an |
| <code>int</code> or <code>unsigned int</code> by the default argument promotions |
| anyway, but the ‘<samp><span class="samp">h</span></samp>’ modifier says to convert it back to a |
| <code>short</code> again. |
| |
| <br><dt>‘<samp><span class="samp">j</span></samp>’<dd>Specifies that the argument is a <code>intmax_t</code> or <code>uintmax_t</code>, as |
| appropriate. |
| |
| <p>This modifier was introduced in ISO C99<!-- /@w -->. |
| |
| <br><dt>‘<samp><span class="samp">l</span></samp>’<dd>Specifies that the argument is a <code>long int</code> or <code>unsigned long |
| int</code>, as appropriate. Two ‘<samp><span class="samp">l</span></samp>’ characters is like the ‘<samp><span class="samp">L</span></samp>’ |
| modifier, below. |
| |
| <p>If used with ‘<samp><span class="samp">%c</span></samp>’ or ‘<samp><span class="samp">%s</span></samp>’ the corresponding parameter is |
| considered as a wide character or wide character string respectively. |
| This use of ‘<samp><span class="samp">l</span></samp>’ was introduced in Amendment 1<!-- /@w --> to ISO C90<!-- /@w -->. |
| |
| <br><dt>‘<samp><span class="samp">L</span></samp>’<dt>‘<samp><span class="samp">ll</span></samp>’<dt>‘<samp><span class="samp">q</span></samp>’<dd>Specifies that the argument is a <code>long long int</code>. (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 <code>long int</code>.) |
| |
| <p>The ‘<samp><span class="samp">q</span></samp>’ modifier is another name for the same thing, which comes |
| from 4.4 BSD; a <code>long long int</code><!-- /@w --> is sometimes called a “quad” |
| <code>int</code>. |
| |
| <br><dt>‘<samp><span class="samp">t</span></samp>’<dd>Specifies that the argument is a <code>ptrdiff_t</code>. |
| |
| <p>This modifier was introduced in ISO C99<!-- /@w -->. |
| |
| <br><dt>‘<samp><span class="samp">z</span></samp>’<dt>‘<samp><span class="samp">Z</span></samp>’<dd>Specifies that the argument is a <code>size_t</code>. |
| |
| <p>‘<samp><span class="samp">z</span></samp>’ was introduced in ISO C99<!-- /@w -->. ‘<samp><span class="samp">Z</span></samp>’ is a GNU extension |
| predating this addition and should not be used in new code. |
| </dl> |
| |
| <p>Here is an example. Using the template string: |
| |
| <pre class="smallexample"> "|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n" |
| </pre> |
| <p class="noindent">to print numbers using the different options for the ‘<samp><span class="samp">%d</span></samp>’ |
| conversion gives results like: |
| |
| <pre class="smallexample"> | 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| |
| </pre> |
| <p>In particular, notice what happens in the last case where the number |
| is too large to fit in the minimum field width specified. |
| |
| <p>Here are some more examples showing how unsigned integers print under |
| various format options, using the template string: |
| |
| <pre class="smallexample"> "|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n" |
| </pre> |
| <pre class="smallexample"> | 0| 0| 0| 0| 0| 0| 0| 00000000| |
| | 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001| |
| |100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0| |
| </pre> |
| </body></html> |
| |