| <html lang="en"> |
| <head> |
| <title>Numeric Input 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-Input.html#Formatted-Input" title="Formatted Input"> |
| <link rel="prev" href="Table-of-Input-Conversions.html#Table-of-Input-Conversions" title="Table of Input Conversions"> |
| <link rel="next" href="String-Input-Conversions.html#String-Input-Conversions" title="String Input 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="Numeric-Input-Conversions"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="String-Input-Conversions.html#String-Input-Conversions">String Input Conversions</a>, |
| Previous: <a rel="previous" accesskey="p" href="Table-of-Input-Conversions.html#Table-of-Input-Conversions">Table of Input Conversions</a>, |
| Up: <a rel="up" accesskey="u" href="Formatted-Input.html#Formatted-Input">Formatted Input</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">12.14.4 Numeric Input Conversions</h4> |
| |
| <p>This section describes the <code>scanf</code> conversions for reading numeric |
| values. |
| |
| <p>The ‘<samp><span class="samp">%d</span></samp>’ conversion matches an optionally signed integer in decimal |
| radix. The syntax that is recognized is the same as that for the |
| <code>strtol</code> function (see <a href="Parsing-of-Integers.html#Parsing-of-Integers">Parsing of Integers</a>) with the value |
| <code>10</code> for the <var>base</var> argument. |
| |
| <p>The ‘<samp><span class="samp">%i</span></samp>’ 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 |
| <code>strtol</code> function (see <a href="Parsing-of-Integers.html#Parsing-of-Integers">Parsing of Integers</a>) with the value |
| <code>0</code> for the <var>base</var> argument. (You can print integers in this |
| syntax with <code>printf</code> by using the ‘<samp><span class="samp">#</span></samp>’ flag character with the |
| ‘<samp><span class="samp">%x</span></samp>’, ‘<samp><span class="samp">%o</span></samp>’, or ‘<samp><span class="samp">%d</span></samp>’ conversion. See <a href="Integer-Conversions.html#Integer-Conversions">Integer Conversions</a>.) |
| |
| <p>For example, any of the strings ‘<samp><span class="samp">10</span></samp>’, ‘<samp><span class="samp">0xa</span></samp>’, or ‘<samp><span class="samp">012</span></samp>’ |
| could be read in as integers under the ‘<samp><span class="samp">%i</span></samp>’ conversion. Each of |
| these specifies a number with decimal value <code>10</code>. |
| |
| <p>The ‘<samp><span class="samp">%o</span></samp>’, ‘<samp><span class="samp">%u</span></samp>’, and ‘<samp><span class="samp">%x</span></samp>’ conversions match unsigned |
| integers in octal, decimal, and hexadecimal radices, respectively. The |
| syntax that is recognized is the same as that for the <code>strtoul</code> |
| function (see <a href="Parsing-of-Integers.html#Parsing-of-Integers">Parsing of Integers</a>) with the appropriate value |
| (<code>8</code>, <code>10</code>, or <code>16</code>) for the <var>base</var> argument. |
| |
| <p>The ‘<samp><span class="samp">%X</span></samp>’ conversion is identical to the ‘<samp><span class="samp">%x</span></samp>’ conversion. They |
| both permit either uppercase or lowercase letters to be used as digits. |
| |
| <p>The default type of the corresponding argument for the <code>%d</code> and |
| <code>%i</code> conversions is <code>int *</code>, and <code>unsigned int *</code> for the |
| other integer conversions. You can use the following type modifiers to |
| specify other sizes of integer: |
| |
| <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>. |
| |
| <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>. |
| |
| <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>. |
| |
| <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>. 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 pointer to 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">ll</span></samp>’<dt>‘<samp><span class="samp">L</span></samp>’<dt>‘<samp><span class="samp">q</span></samp>’<dd>Specifies that the argument is a <code>long long int *</code> or <code>unsigned long long int *</code>. (The <code>long long</code> type is an extension supported by the |
| GNU C compiler. For systems that don't provide 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>’<dd>Specifies that the argument is a <code>size_t *</code>. |
| |
| <p>This modifier was introduced in ISO C99<!-- /@w -->. |
| </dl> |
| |
| <p>All of the ‘<samp><span class="samp">%e</span></samp>’, ‘<samp><span class="samp">%f</span></samp>’, ‘<samp><span class="samp">%g</span></samp>’, ‘<samp><span class="samp">%E</span></samp>’, and ‘<samp><span class="samp">%G</span></samp>’ |
| input conversions are interchangeable. They all match an optionally |
| signed floating point number, in the same syntax as for the |
| <code>strtod</code> function (see <a href="Parsing-of-Floats.html#Parsing-of-Floats">Parsing of Floats</a>). |
| |
| <p>For the floating-point input conversions, the default argument type is |
| <code>float *</code>. (This is different from the corresponding output |
| conversions, where the default type is <code>double</code>; remember that |
| <code>float</code> arguments to <code>printf</code> are converted to <code>double</code> |
| by the default argument promotions, but <code>float *</code> arguments are |
| not promoted to <code>double *</code>.) You can specify other sizes of float |
| using these type modifiers: |
| |
| <dl> |
| <dt>‘<samp><span class="samp">l</span></samp>’<dd>Specifies that the argument is of type <code>double *</code>. |
| |
| <br><dt>‘<samp><span class="samp">L</span></samp>’<dd>Specifies that the argument is of type <code>long double *</code>. |
| </dl> |
| |
| <p>For all the above number parsing formats there is an additional optional |
| flag ‘<samp><span class="samp">'</span></samp>’. When this flag is given the <code>scanf</code> function |
| expects the number represented in the input string to be formatted |
| according to the grouping rules of the currently selected locale |
| (see <a href="General-Numeric.html#General-Numeric">General Numeric</a>). |
| |
| <p>If the <code>"C"</code> or <code>"POSIX"</code> 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. |
| |
| </body></html> |
| |