174 lines
8.7 KiB
HTML
174 lines
8.7 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>scanf(3)</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<H1>scanf(3)</H1>
|
|
<HR>
|
|
<PRE>
|
|
|
|
</PRE>
|
|
<H2>NAME</H2><PRE>
|
|
scanf, fscanf, sscanf, vscanf, vfscanf, vsscanf - formatted input
|
|
conversion
|
|
|
|
|
|
</PRE>
|
|
<H2>SYNOPSIS</H2><PRE>
|
|
<STRONG>#include</STRONG> <STRONG><stdio.h></STRONG>
|
|
<STRONG>#include</STRONG> <STRONG><stdarg.h></STRONG>
|
|
|
|
<STRONG>int</STRONG> <STRONG>scanf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>pointer</EM>] ...<STRONG>)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>fscanf(FILE</STRONG> <STRONG>*</STRONG><EM>stream</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>pointer</EM>] ...<STRONG>)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>sscanf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM> [<STRONG>,</STRONG> <EM>pointer</EM>] ...<STRONG>)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vscanf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vfscanf(FILE</STRONG> <STRONG>*</STRONG><EM>stream</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>)</STRONG>
|
|
<STRONG>int</STRONG> <STRONG>vsscanf(const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>s</EM><STRONG>,</STRONG> <STRONG>const</STRONG> <STRONG>char</STRONG> <STRONG>*</STRONG><EM>format</EM><STRONG>,</STRONG> <STRONG>va_list</STRONG> <EM>args</EM><STRONG>)</STRONG>
|
|
|
|
|
|
</PRE>
|
|
<H2>DESCRIPTION</H2><PRE>
|
|
<STRONG>Scanf</STRONG> reads from the standard input stream <STRONG>stdin</STRONG>. <STRONG>Fscanf</STRONG> reads from the
|
|
named input <EM>stream</EM>. <STRONG>Sscanf</STRONG> reads from the character string <EM>s</EM>. Each
|
|
function reads characters, interprets them according to a format, and
|
|
stores the results in its arguments. Each expects as arguments a control
|
|
string <EM>format</EM>, described below, and a set of <EM>pointer</EM> arguments indicating
|
|
where the converted input should be stored.
|
|
|
|
The <STRONG>v*scanf</STRONG> functions can be used to make functions like the first three
|
|
by using the <STRONG><A HREF="../man3/stdarg.3.html">stdarg(3)</A></STRONG> method to process the argument pointers.
|
|
|
|
The control string usually contains conversion specifications, which are
|
|
used to direct interpretation of input sequences. The control string may
|
|
contain:
|
|
|
|
1. Blanks, tabs or newlines, which match optional white space in the
|
|
input.
|
|
|
|
2. An ordinary character (not %) which must match the next character of
|
|
the input stream.
|
|
|
|
3. Conversion specifications, consisting of the character <STRONG>%</STRONG>, an optional
|
|
assignment suppressing character <STRONG>*</STRONG>, an optional numerical maximum
|
|
field width, and a conversion character.
|
|
|
|
A conversion specification directs the conversion of the next input
|
|
field; the result is placed in the variable pointed to by the
|
|
corresponding argument, unless assignment suppression was indicated by <STRONG>*</STRONG>.
|
|
An input field is defined as a string of non-space characters; it extends
|
|
to the next inappropriate character or until the field width, if
|
|
specified, is exhausted.
|
|
|
|
The conversion character indicates the interpretation of the input field;
|
|
the corresponding pointer argument must usually be of a restricted type.
|
|
The following conversion characters are legal:
|
|
<STRONG>%</STRONG> a single `%' is expected in the input at this point; no assignment is
|
|
done.
|
|
|
|
<STRONG>d</STRONG> a decimal integer is expected; the corresponding argument should be
|
|
an integer pointer.
|
|
|
|
<STRONG>o</STRONG> an octal integer is expected; the corresponding argument should be a
|
|
integer pointer.
|
|
|
|
<STRONG>x</STRONG> a hexadecimal integer is expected; the corresponding argument should
|
|
be an integer pointer.
|
|
|
|
<STRONG>s</STRONG> a character string is expected; the corresponding argument should be
|
|
a character pointer pointing to an array of characters large enough
|
|
to accept the string and a terminating `\0', which will be added.
|
|
The input field is terminated by a space character or a newline.
|
|
|
|
<STRONG>c</STRONG> a character is expected; the corresponding argument should be a
|
|
character pointer. The normal skip over space characters is
|
|
suppressed in this case; to read the next non-space character, try
|
|
`%1s'. If a field width is given, the corresponding argument should
|
|
refer to a character array, and the indicated number of characters is
|
|
read.
|
|
|
|
<STRONG>efg</STRONG> a floating point number is expected; the next field is converted
|
|
accordingly and stored through the corresponding argument, which
|
|
should be a pointer to a <STRONG>float</STRONG>. The input format for floating point
|
|
numbers is an optionally signed string of digits possibly containing
|
|
a decimal point, followed by an optional exponent field consisting of
|
|
an E or e followed by an optionally signed integer.
|
|
|
|
<STRONG>[</STRONG> indicates a string not to be delimited by space characters. The left
|
|
bracket is followed by a set of characters and a right bracket; the
|
|
characters between the brackets define a set of characters making up
|
|
the string. If the first character is not circumflex (^), the input
|
|
field is all characters until the first character not in the set
|
|
between the brackets; if the first character after the left bracket
|
|
is ^, the input field is all characters until the first character
|
|
which is in the remaining set of characters between the brackets.
|
|
The corresponding argument must point to a character array.
|
|
|
|
The conversion characters <STRONG>d</STRONG>, <STRONG>o</STRONG> and <STRONG>x</STRONG> may be capitalized or preceded by <STRONG>l</STRONG>
|
|
to indicate that a pointer to <STRONG>long</STRONG> rather than to <STRONG>int</STRONG> is in the argument
|
|
list. Similarly, the conversion characters <STRONG>e</STRONG>, <STRONG>f</STRONG> or <STRONG>g</STRONG> may be capitalized
|
|
or preceded by <STRONG>l</STRONG> to indicate a pointer to <STRONG>double</STRONG> rather than to <STRONG>float</STRONG>.
|
|
The conversion characters <STRONG>d</STRONG>, <STRONG>o</STRONG> and <STRONG>x</STRONG> may be preceded by <STRONG>h</STRONG> to indicate a
|
|
pointer to <STRONG>short</STRONG> rather than to <STRONG>int</STRONG>.
|
|
|
|
|
|
|
|
The <STRONG>scanf</STRONG> functions return the number of successfully matched and
|
|
assigned input items. This can be used to decide how many input items
|
|
were found. The constant <STRONG>EOF</STRONG> is returned upon end of input; note that
|
|
this is different from 0, which means that no conversion was done; if
|
|
conversion was intended, it was frustrated by an inappropriate character
|
|
in the input.
|
|
|
|
For example, the call
|
|
|
|
int i; float x; char name[50];
|
|
scanf("%d%f%s", &i, &x, name);
|
|
|
|
with the input line
|
|
|
|
25 54.32E-1 thompson
|
|
|
|
will assign to <STRONG>i</STRONG> the value 25, <STRONG>x</STRONG> the value 5.432, and <STRONG>name</STRONG> will contain
|
|
`<STRONG>thompson\0</STRONG>' . Or,
|
|
|
|
int i; float x; char name[50];
|
|
scanf("%2d%f%*d%[1234567890]", &i, &x, name);
|
|
|
|
with input
|
|
|
|
56789 0123 56a72
|
|
|
|
will assign 56 to <STRONG>i</STRONG>, 789.0 to <STRONG>x</STRONG>, skip `0123', and place the string `56\0'
|
|
in <STRONG>name</STRONG>. The next call to <STRONG>getchar</STRONG> will return `a'.
|
|
|
|
|
|
</PRE>
|
|
<H2>SEE ALSO</H2><PRE>
|
|
<STRONG><A HREF="../man3/atof.3.html">atof(3)</A></STRONG>, <STRONG><A HREF="../man3/getc.3.html">getc(3)</A></STRONG>, <STRONG><A HREF="../man3/printf.3.html">printf(3)</A></STRONG>, <STRONG><A HREF="../man3/stdarg.3.html">stdarg(3)</A></STRONG>.
|
|
|
|
|
|
</PRE>
|
|
<H2>DIAGNOSTICS</H2><PRE>
|
|
The <STRONG>scanf</STRONG> functions return <STRONG>EOF</STRONG> on end of input, and a short count for
|
|
missing or illegal data items.
|
|
|
|
|
|
</PRE>
|
|
<H2>BUGS</H2><PRE>
|
|
The success of literal matches and suppressed assignments is not directly
|
|
determinable.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</PRE>
|
|
</BODY>
|
|
</HTML>
|