|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>sscanf (The Red Hat newlib C Library)</title>
-
- <meta name="description" content="sscanf (The Red Hat newlib C Library)">
- <meta name="keywords" content="sscanf (The Red Hat newlib C Library)">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
- <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="Stdio.html#Stdio" rel="up" title="Stdio">
- <link href="stdio_005fext.html#stdio_005fext" rel="next" title="stdio_ext">
- <link href="sprintf.html#sprintf" rel="prev" title="sprintf">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="sscanf"></a>
- <div class="header">
- <p>
- Next: <a href="stdio_005fext.html#stdio_005fext" accesskey="n" rel="next">stdio_ext</a>, Previous: <a href="sprintf.html#sprintf" accesskey="p" rel="prev">sprintf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
- </div>
- <hr>
- <a name="sscanf_002c-fscanf_002c-scanf_002d_002d_002dscan-and-format-input"></a>
- <h3 class="section">4.62 <code>sscanf</code>, <code>fscanf</code>, <code>scanf</code>—scan and format input</h3>
- <a name="index-scanf"></a>
- <a name="index-_005fscanf_005fr"></a>
- <a name="index-fscanf"></a>
- <a name="index-_005ffscanf_005fr"></a>
- <a name="index-sscanf"></a>
- <a name="index-_005fsscanf_005fr"></a>
- <p><strong>Synopsis</strong>
- </p><div class="example">
- <pre class="example">#include <stdio.h>
-
- int scanf(const char *restrict <var>format</var>, ...);
- int fscanf(FILE *restrict <var>fd</var>, const char *restrict <var>format</var>, ...);
- int sscanf(const char *restrict <var>str</var>, const char *restrict <var>format</var>, ...);
-
- int _scanf_r(struct _reent *<var>ptr</var>, const char *restrict <var>format</var>, ...);
- int _fscanf_r(struct _reent *<var>ptr</var>, FILE *restrict <var>fd</var>,
- const char *restrict <var>format</var>, ...);
- int _sscanf_r(struct _reent *<var>ptr</var>, const char *restrict <var>str</var>,
- const char *restrict <var>format</var>, ...);
-
- </pre></div>
- <p><strong>Description</strong><br>
- <code>scanf</code> scans a series of input fields from standard input,
- one character at a time. Each field is interpreted according to
- a format specifier passed to <code>scanf</code> in the format string at
- <code>*<var>format</var></code>. <code>scanf</code> stores the interpreted input from
- each field at the address passed to it as the corresponding argument
- following <var>format</var>. You must supply the same number of
- format specifiers and address arguments as there are input fields.
- </p>
- <p>There must be sufficient address arguments for the given format
- specifiers; if not the results are unpredictable and likely
- disasterous. Excess address arguments are merely ignored.
- </p>
- <p><code>scanf</code> often produces unexpected results if the input diverges from
- an expected pattern. Since the combination of <code>gets</code> or <code>fgets</code>
- followed by <code>sscanf</code> is safe and easy, that is the preferred way
- to be certain that a program is synchronized with input at the end
- of a line.
- </p>
- <p><code>fscanf</code> and <code>sscanf</code> are identical to <code>scanf</code>, other than the
- source of input: <code>fscanf</code> reads from a file, and <code>sscanf</code>
- from a string.
- </p>
- <p>The routines <code>_scanf_r</code>, <code>_fscanf_r</code>, and <code>_sscanf_r</code> are reentrant
- versions of <code>scanf</code>, <code>fscanf</code>, and <code>sscanf</code> that take an additional
- first argument pointing to a reentrancy structure.
- </p>
- <p>The string at <code>*<var>format</var></code> is a character sequence composed
- of zero or more directives. Directives are composed of
- one or more whitespace characters, non-whitespace characters,
- and format specifications.
- </p>
- <p>Whitespace characters are blank (<code> </code>), tab (<code>\t</code>), or
- newline (<code>\n</code>).
- When <code>scanf</code> encounters a whitespace character in the format string
- it will read (but not store) all consecutive whitespace characters
- up to the next non-whitespace character in the input.
- </p>
- <p>Non-whitespace characters are all other ASCII characters except the
- percent sign (<code>%</code>). When <code>scanf</code> encounters a non-whitespace
- character in the format string it will read, but not store
- a matching non-whitespace character.
- </p>
- <p>Format specifications tell <code>scanf</code> to read and convert characters
- from the input field into specific types of values, and store then
- in the locations specified by the address arguments.
- </p>
- <p>Trailing whitespace is left unread unless explicitly
- matched in the format string.
- </p>
- <p>The format specifiers must begin with a percent sign (<code>%</code>)
- and have the following form:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> %[*][<var>width</var>][<var>size</var>]<var>type</var>
- </pre></div>
-
- <p>Each format specification begins with the percent character (<code>%</code>).
- The other fields are:
- </p><ul>
- <li> *
-
- <p>an optional marker; if present, it suppresses interpretation and
- assignment of this input field.
- </p>
- </li><li> <var>width</var>
-
- <p>an optional maximum field width: a decimal integer,
- which controls the maximum number of characters that
- will be read before converting the current input field. If the
- input field has fewer than <var>width</var> characters, <code>scanf</code>
- reads all the characters in the field, and then
- proceeds with the next field and its format specification.
- </p>
- <p>If a whitespace or a non-convertable character occurs
- before <var>width</var> character are read, the characters up
- to that character are read, converted, and stored.
- Then <code>scanf</code> proceeds to the next format specification.
- </p>
- </li><li> <var>size</var>
-
- <p><code>h</code>, <code>j</code>, <code>l</code>, <code>L</code>, <code>t</code>, and <code>z</code> are optional size
- characters which override the default way that <code>scanf</code>
- interprets the data type of the corresponding argument.
- </p>
- <table>
- <thead><tr><th width="18%">Modifier</th><th width="30%">Type(s)</th><th width="52%"></th></tr></thead>
- <tr><td width="18%">hh</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to char, store in char object</td></tr>
- <tr><td width="18%">h</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to short, store in short object</td></tr>
- <tr><td width="18%">h</td><td width="30%">D, I, O, U, X, e, f, c, s, p</td><td width="52%">no effect</td></tr>
- <tr><td width="18%">j</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to intmax_t, store in intmax_t object</td></tr>
- <tr><td width="18%">j</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <tr><td width="18%">l</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to long, store in long object</td></tr>
- <tr><td width="18%">l</td><td width="30%">e, f, g</td><td width="52%">convert input to double, store in a double object</td></tr>
- <tr><td width="18%">l</td><td width="30%">D, I, O, U, X, c, s, p</td><td width="52%">no effect</td></tr>
- <tr><td width="18%">ll</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert to long long, store in long long object</td></tr>
- <tr><td width="18%">L</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert to long long, store in long long object</td></tr>
- <tr><td width="18%">L</td><td width="30%">e, f, g, E, G</td><td width="52%">convert to long double, store in long double object</td></tr>
- <tr><td width="18%">L</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <tr><td width="18%">t</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to ptrdiff_t, store in ptrdiff_t object</td></tr>
- <tr><td width="18%">t</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- <tr><td width="18%">z</td><td width="30%">d, i, o, u, x, n</td><td width="52%">convert input to size_t, store in size_t object</td></tr>
- <tr><td width="18%">z</td><td width="30%">all others</td><td width="52%">no effect</td></tr>
- </table>
-
- </li><li> <var>type</var>
-
- <p>A character to specify what kind of conversion
- <code>scanf</code> performs. Here is a table of the conversion
- characters:
- </p>
- <dl compact="compact">
- <dt><code>%</code></dt>
- <dd><p>No conversion is done; the percent character (<code>%</code>) is stored.
- </p>
- </dd>
- <dt><code>c</code></dt>
- <dd><p>Scans one character. Corresponding <var>arg</var>: <code>(char *arg)</code>.
- </p>
- </dd>
- <dt><code>s</code></dt>
- <dd><p>Reads a character string into the array supplied.
- Corresponding <var>arg</var>: <code>(char arg[])</code>.
- </p>
- </dd>
- <dt><code>[<var>pattern</var>]</code></dt>
- <dd><p>Reads a non-empty character string into memory
- starting at <var>arg</var>. This area must be large
- enough to accept the sequence and a
- terminating null character which will be added
- automatically. (<var>pattern</var> is discussed in the paragraph following
- this table). Corresponding <var>arg</var>: <code>(char *arg)</code>.
- </p>
- </dd>
- <dt><code>d</code></dt>
- <dd><p>Reads a decimal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>D</code></dt>
- <dd><p>Reads a decimal integer into the corresponding
- <var>arg</var>: <code>(long *arg)</code>.
- </p>
- </dd>
- <dt><code>o</code></dt>
- <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>O</code></dt>
- <dd><p>Reads an octal integer into the corresponding <var>arg</var>: <code>(long *arg)</code>.
- </p>
- </dd>
- <dt><code>u</code></dt>
- <dd><p>Reads an unsigned decimal integer into the corresponding
- <var>arg</var>: <code>(unsigned int *arg)</code>.
- </p>
- </dd>
- <dt><code>U</code></dt>
- <dd><p>Reads an unsigned decimal integer into the corresponding <var>arg</var>:
- <code>(unsigned long *arg)</code>.
- </p>
- </dd>
- <dt><code>x,X</code></dt>
- <dd><p>Read a hexadecimal integer into the corresponding <var>arg</var>:
- <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>e, f, g</code></dt>
- <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
- <code>(float *arg)</code>.
- </p>
- </dd>
- <dt><code>E, F, G</code></dt>
- <dd><p>Read a floating-point number into the corresponding <var>arg</var>:
- <code>(double *arg)</code>.
- </p>
- </dd>
- <dt><code>i</code></dt>
- <dd><p>Reads a decimal, octal or hexadecimal integer into the
- corresponding <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>I</code></dt>
- <dd><p>Reads a decimal, octal or hexadecimal integer into the
- corresponding <var>arg</var>: <code>(long *arg)</code>.
- </p>
- </dd>
- <dt><code>n</code></dt>
- <dd><p>Stores the number of characters read in the corresponding
- <var>arg</var>: <code>(int *arg)</code>.
- </p>
- </dd>
- <dt><code>p</code></dt>
- <dd><p>Stores a scanned pointer. ANSI C leaves the details
- to each implementation; this implementation treats
- <code>%p</code> exactly the same as <code>%U</code>. Corresponding
- <var>arg</var>: <code>(void **arg)</code>.
- </p></dd>
- </dl>
-
-
- <p>A <var>pattern</var> of characters surrounded by square brackets can be used
- instead of the <code>s</code> type character. <var>pattern</var> is a set of
- characters which define a search set of possible characters making up
- the <code>scanf</code> input field. If the first character in the brackets is a
- caret (<code>^</code>), the search set is inverted to include all ASCII characters
- except those between the brackets. There is also a range facility
- which you can use as a shortcut. <code>%[0-9] </code> matches all decimal digits.
- The hyphen must not be the first or last character in the set.
- The character prior to the hyphen must be lexically less than the
- character after it.
- </p>
- <p>Here are some <var>pattern</var> examples:
- </p><dl compact="compact">
- <dt><code>%[abcd]</code></dt>
- <dd><p>matches strings containing only <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>.
- </p>
- </dd>
- <dt><code>%[^abcd]</code></dt>
- <dd><p>matches strings containing any characters except <code>a</code>, <code>b</code>,
- <code>c</code>, or <code>d</code>
- </p>
- </dd>
- <dt><code>%[A-DW-Z]</code></dt>
- <dd><p>matches strings containing <code>A</code>, <code>B</code>, <code>C</code>, <code>D</code>, <code>W</code>,
- <code>X</code>, <code>Y</code>, <code>Z</code>
- </p>
- </dd>
- <dt><code>%[z-a]</code></dt>
- <dd><p>matches the characters <code>z</code>, <code>-</code>, and <code>a</code>
- </p></dd>
- </dl>
-
-
- <p>Floating point numbers (for field types <code>e</code>, <code>f</code>, <code>g</code>, <code>E</code>,
- <code>F</code>, <code>G</code>) must correspond to the following general form:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> [+/-] ddddd[.]ddd [E|e[+|-]ddd]
- </pre></div>
-
- <p>where objects inclosed in square brackets are optional, and <code>ddd</code>
- represents decimal, octal, or hexadecimal digits.
- </p></li></ul>
-
-
- <br>
- <p><strong>Returns</strong><br>
- <code>scanf</code> returns the number of input fields successfully
- scanned, converted and stored; the return value does
- not include scanned fields which were not stored.
- </p>
- <p>If <code>scanf</code> attempts to read at end-of-file, the return
- value is <code>EOF</code>.
- </p>
- <p>If no fields were stored, the return value is <code>0</code>.
- </p>
- <p><code>scanf</code> might stop scanning a particular field before
- reaching the normal field end character, or may
- terminate entirely.
- </p>
- <p><code>scanf</code> stops scanning and storing the current field
- and moves to the next input field (if any)
- in any of the following situations:
- </p>
- <ul>
- <li> The assignment suppressing character (<code>*</code>) appears
- after the <code>%</code> in the format specification; the current
- input field is scanned but not stored.
-
- </li><li> <var>width</var> characters have been read (<var>width</var> is a
- width specification, a positive decimal integer).
-
- </li><li> The next character read cannot be converted
- under the the current format (for example,
- if a <code>Z</code> is read when the format is decimal).
-
- </li><li> The next character in the input field does not appear
- in the search set (or does appear in the inverted search set).
- </li></ul>
-
-
- <p>When <code>scanf</code> stops scanning the current input field for one of
- these reasons, the next character is considered unread and
- used as the first character of the following input field, or the
- first character in a subsequent read operation on the input.
- </p>
- <p><code>scanf</code> will terminate under the following circumstances:
- </p>
- <ul>
- <li> The next character in the input field conflicts
- with a corresponding non-whitespace character in the
- format string.
-
- </li><li> The next character in the input field is <code>EOF</code>.
-
- </li><li> The format string has been exhausted.
- </li></ul>
-
-
- <p>When the format string contains a character sequence that is
- not part of a format specification, the same character
- sequence must appear in the input; <code>scanf</code> will
- scan but not store the matched characters. If a
- conflict occurs, the first conflicting character remains in the input
- as if it had never been read.
- </p>
- <br>
- <p><strong>Portability</strong><br>
- <code>scanf</code> is ANSI C.
- </p>
- <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
- <code>lseek</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
- </p>
- <br>
-
- <hr>
- <div class="header">
- <p>
- Next: <a href="stdio_005fext.html#stdio_005fext" accesskey="n" rel="next">stdio_ext</a>, Previous: <a href="sprintf.html#sprintf" accesskey="p" rel="prev">sprintf</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
- </div>
-
-
-
- </body>
- </html>
|