input format conversion

You're viewing an older version of this page (#3282). View the current version.

View versions (3)

NAME

<span class="Nm" id="scanf">scanf</span>, <span class="Nm" id="fscanf">fscanf</span>, <span class="Nm" id="sscanf">sscanf</span>, <span class="Nm" id="vscanf">vscanf</span>, <span class="Nm" id="vsscanf">vsscanf</span>, <span class="Nm" id="vfscanf">vfscanf</span>

INTERFACE

#include <stdio.h> int scanf ( const char * restrict format, ... ) int fscanf ( FILE * restrict stream, const char * restrict format, ... ) int sscanf ( const char * restrict str, const char * restrict format, ... ) #include <stdarg.h> int vscanf ( const char * restrict format, va_list ap ) int vsscanf ( const char * restrict str, const char * restrict format, va_list ap ) int vfscanf ( FILE * restrict stream, const char * restrict format, va_list ap )

DESCRIPTION

The scanf family of functions scans input according to a &#92;c format as described below. This format may contain <span class="Em">conversion</span> <span class="Em">specifiers</span>; the results from such conversions, if any, are stored through the <span class="Em">pointer</span> arguments. The scanf function reads input from the standard input stream <span class="Dv">stdin</span>, fscanf reads input from the stream pointer &#92;c stream, and sscanf reads its input from the character string pointed to by &#92;c str. The vfscanf function is analogous to reference:vfprintf (3) and reads input from the stream pointer &#92;c stream using a variable argument list of pointers (see reference:stdarg (3) ). The vscanf function scans a variable argument list from the standard input and the vsscanf function scans it from a string; these are analogous to the vprintf and vsprintf functions respectively. Each successive <span class="Em">pointer</span> argument must correspond properly with each successive conversion specifier (but see the <span class="Cm">*</span> and <span class="Cm">%n</span> conversions below). All conversions are introduced by the  <span class="Cm">%</span> (percent sign) character or  <span class="Cm">%n</span> sequence. #include <the> <span class="Em">pointer</span> will be the <span class="Cm">n</span> th argument after the format string. The &#92;c format string may also contain other characters. White space (such as blanks, tabs, or newlines) in the &#92;c format string match any amount of white space, including none, in the input. Everything else matches only itself. Scanning stops when an input character does not match such a format character. Scanning also stops when an input conversion cannot be made (see below). &#92;n &#92;n Extended locale versions of these functions are documented in reference:scanf_l (3) . See reference:xlocale (3) for more information.

CONVERSIONS

Following the <span class="Cm">%</span> character or <span class="Cm">%n<div class="orangebox">[$]</div></span> sequence introducing a conversion there may be a number of <span class="Em">flag</span> characters, as follows: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Cm">*</span> </td> <td valign="top"> Suppresses assignment. The conversion that follows occurs as usual, but no pointer is used; the result of the conversion is simply discarded. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">hh</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">char</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">h</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">short int</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">l</span> (ell) </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long int</span> (rather than <span class="Vt">int</span>), that the conversion will be one of <span class="Cm">a</span>, <span class="Cm">e</span>, <span class="Cm">f</span>, or <span class="Cm">g</span> and the next pointer is a pointer to <span class="Vt">double</span> (rather than <span class="Vt">float</span>), or that the conversion will be one of <span class="Cm">c</span>, <span class="Cm">s</span> or <span class="Cm">[</span> and the next pointer is a pointer to an array of <span class="Vt">wchar_t</span> (rather than <span class="Vt">char</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">ll</span> (ell ell) </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long long int</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">L</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">a</span>, <span class="Cm">e</span>, <span class="Cm">f</span>, or <span class="Cm">g</span> and the next pointer is a pointer to <span class="Vt">long double</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">j</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">intmax_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">t</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">ptrdiff_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">z</span> </td> <td valign="top"> Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">size_t</span> (rather than <span class="Vt">int</span>). </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">q</span> </td> <td valign="top"> (deprecated.) Indicates that the conversion will be one of <span class="Cm">dioux</span> or <span class="Cm">n</span> and the next pointer is a pointer to a <span class="Vt">long long int</span> (rather than <span class="Vt">int</span>). </td> </tr> </table> &#92;n &#92;n In addition to these flags, there may be an optional maximum field width, expressed as a decimal integer, between the <span class="Cm">%</span> and the conversion. If no width is given, a default of "<span class="Dq">infinity</span>" is used (with one exception, below); otherwise at most this many bytes are scanned in processing the conversion. In the case of the <span class="Cm">lc</span>, <span class="Cm">ls</span> and <span class="Cm">l[</span> conversions, the field width specifies the maximum number of multibyte characters that will be scanned. Before conversion begins, most conversions skip white space; this white space is not counted against the field width. &#92;n &#92;n The following conversions are available: <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Cm">%</span> </td> <td valign="top"> Matches a literal '<span class="Qlq">%</span>'. That is, "<span class="Dq"><span class="Li">%%</span></span>" in the format string matches a single input '<span class="Qlq">%</span>' character. No conversion is done, and assignment does not occur. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">d</span> </td> <td valign="top"> Matches an optionally signed decimal integer; the next pointer must be a pointer to <span class="Vt">int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">i</span> </td> <td valign="top"> Matches an optionally signed integer; the next pointer must be a pointer to <span class="Vt">int</span>. The integer is read in base 16 if it begins with '<span class="Qlq">0x</span>' or '<span class="Qlq">0X</span>', in base 8 if it begins with '<span class="Qlq">0</span>', and in base 10 otherwise. Only characters that correspond to the base are used. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">o</span> </td> <td valign="top"> Matches an octal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">u</span> </td> <td valign="top"> Matches an optionally signed decimal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">x</span>, <span class="Cm">X</span> </td> <td valign="top"> Matches an optionally signed hexadecimal integer; the next pointer must be a pointer to <span class="Vt">unsigned int</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">a</span>, <span class="Cm">A</span>, <span class="Cm">e</span>, <span class="Cm">E</span>, <span class="Cm">f</span>, <span class="Cm">F</span>, <span class="Cm">g</span>, <span class="Cm">G</span> </td> <td valign="top"> Matches a floating-point number in the style of strtod (3) . The next pointer must be a pointer to <span class="Vt">float</span> (unless <span class="Cm">l</span> or <span class="Cm">L</span> is specified.) </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">s</span> </td> <td valign="top"> Matches a sequence of non-white-space characters; the next pointer must be a pointer to <span class="Vt">char</span>, and the array must be large enough to accept all the sequence and the terminating <span class="Dv">NUL</span> character. The input string stops at white space or at the maximum field width, whichever occurs first. &#92;n &#92;n If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by reference:mbrtowc (3) . </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">S</span> </td> <td valign="top"> The same as <span class="Cm">ls</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">c</span> </td> <td valign="top"> Matches a sequence of <span class="Em">width</span> count characters (default 1); the next pointer must be a pointer to <span class="Vt">char</span>, and there must be enough room for all the characters (no terminating <span class="Dv">NUL</span> is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format. &#92;n &#92;n If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by reference:mbrtowc (3) . </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">C</span> </td> <td valign="top"> The same as <span class="Cm">lc</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">[</span> </td> <td valign="top"> Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to <span class="Vt">char</span>, and there must be enough room for all the characters in the string, plus a terminating <span class="Dv">NUL</span> character. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket <span class="Cm">[</span> character and a close bracket <span class="Cm">]</span> character. The set <span class="Em">excludes</span> those characters if the first character after the open bracket is a circumflex <span class="Cm">^</span>. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character <span class="Cm">-</span> is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, '<span class="Qlq">[^]0-9-]</span>' means the set "<span class="Dq">everything except close bracket, zero through nine, and hyphen</span>". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out. &#92;n &#92;n If an <span class="Cm">l</span> qualifier is present, the next pointer must be a pointer to <span class="Vt">wchar_t</span>, into which the input will be placed after conversion by reference:mbrtowc (3) . </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">p</span> </td> <td valign="top"> Matches a pointer value (as printed by '<span class="Qlq">%p</span>' in printf (3) ); the next pointer must be a pointer to <span class="Vt">void</span>. </td> </tr> <tr> <td valign="top" nowrap> <span class="Cm">n</span> </td> <td valign="top"> Nothing is expected; instead, the number of characters consumed thus far from the input is stored through the next pointer, which must be a pointer to <span class="Vt">int</span>. This is <span class="Em">not</span> a conversion, although it can be suppressed with the <span class="Cm">*</span> flag. </td> </tr> </table> &#92;n &#92;n The decimal point character is defined in the program's locale (category <span class="Dv">LC_NUMERIC</span>). &#92;n &#92;n For backwards compatibility, a "<span class="Dq">conversion</span>" of '<span class="Qlq">%\\0</span>' causes an immediate return of <span class="Dv">EOF</span>.

RETURN VALUES

These functions return the number of input items assigned, which can be fewer than provided for, or even zero, in the event of a matching failure. Zero indicates that, while there was input available, no conversions were assigned; typically this is due to an invalid input character, such as an alphabetic character for a '<span class="Qlq">%d</span>' conversion. The value <span class="Dv">EOF</span> is returned if an input failure occurs before any conversion such as an end-of-file occurs. If an error or end-of-file occurs after conversion has begun, the number of conversions which were successfully completed is returned.

SEE ALSO

getc (3) , reference:mbrtowc (3) , printf (3) , strtod (3) , strtol (3) , strtoul (3) , reference:wscanf (3) , reference:scanf_l (3)

STANDARDS

The functions fscanf , scanf , sscanf , vfscanf , vscanf and vsscanf conform to

BUGS

Earlier implementations of scanf treated <span class="Cm">%D</span>, <span class="Cm">%E</span>, <span class="Cm">%F</span>, <span class="Cm">%O</span> and <span class="Cm">%X</span> as their lowercase equivalents with an <span class="Cm">l</span> modifier. In addition, scanf treated an unknown conversion character as <span class="Cm">%d</span> or <span class="Cm">%D</span>, depending on its case. This functionality has been removed. &#92;n &#92;n Numerical strings are truncated to 512 characters; for example, <span class="Cm">%f</span> and <span class="Cm">%d</span> are implicitly <span class="Cm">%512f</span> and <span class="Cm">%512d</span>. &#92;n &#92;n The scanf family of functions do not correctly handle multibyte characters in the &#92;c format argument.