.so header
.tl /SCANF(3C)/Kreider OS-9 C Library/SCANF(3C)/
.sh NAME
.bb
scanf, fscanf, sscanf - convert formatted input
.eb
.sh SYNOPSIS
.bb
#include <stdio.h>
.sp
int scanf (format [, pointer...])
char *format;
.PP
int fscanf (fp, format [, pointer...])
FILE *fp;
char *format;
.sp
int sscanf (s, format [, pointer...])
char *s, *format;
.eb
.sh DESCRIPTION
.db
The \fBSCANF\fP subroutine reads from the standard input stream \fIstdin.\fP The \fBFSCANF\fP subroutine reads from the named input \fIfp.\fP The \fBSSCANF\fP subroutine reads from the character string \fIs.\fP Each function reads characters, interprets
them according to a format, and stores the results in its arguments.
Each expects, as arguments, a control string \fIformat\fP described below, and a set of \fIpointer\fP arguments
indicating where the converted input should be stored.
.PP
The control string usually contains conversion specifications,
which are used to direct interpretation of input sequences.
The control string may contain:
.bb
White-space characters
which, except in two cases described later,
cause input to be read up to the next
non-white-space character.
.eb
An ordinary character (not %),
which must match the next character of the input stream.
.PP
Conversion specifications, consisting of the character
% ,
an optional assignment suppressing character
\(** ,
an optional numerical maximum field width, an optional
l " (ell) or " h
indicating the size of the receiving variable, and a conversion code.
.PP
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
\(**.
The suppression of assignment provides a way of describing an
input field which is to be skipped.
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.
For all descriptors except left-bracket ([) and c,
white space leading an input field is ignored.
.PP
The conversion code indicates the interpretation of the
input field. The corresponding pointer argument must
usually be a restricted type.
For a suppressed field, a pointer argument is not given.
The following conversion codes are legal:
.PP
%
A single
%
is expected in the input at this point; no assignment is done.
.sp
d
A decimal integer is expected;
the corresponding argument should be an integer pointer.
.sp
u
An unsigned decimal integer is expected;
the corresponding argument should be an unsigned integer pointer.
.sp
o
An octal integer is expected;
the corresponding argument should be an integer pointer.
.sp
x
A hexadecimal integer is expected;
the corresponding argument should be an integer pointer.
.sp
e,f,g
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
float.
The input format for floating point numbers is an optionally signed
string of digits, possibly containing a radix character, followed by
an optional exponent field consisting of an
E
or an
e ,
followed by an optional \(pl, \-, or space, followed by an integer.
.sp
s
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
\e0 ,
which is added automatically.
The input field is terminated by a white-space character.
.sp
c
A character is expected; the
corresponding argument should be a character pointer.
The normal skip over white space is suppressed in this case;
to read the next non-space character, use
%1s.
If a field width is given, the corresponding argument
should refer to a character array; the
indicated number of characters is read.
.PP
.sp
[
Indicates string data and the normal skip over leading white space
is suppressed. The left bracket is followed by a set of characters,
which can be called the
scanset,
and a right bracket.
The input field is the maximal
sequence of input characters consisting entirely of
characters in the scanset.
The circumflex \^,
when it appears as the first character in the scanset,
serves as a complement operator and redefines the scanset as
the set of all characters not contained in the remainder of the scanset string.
There are some conventions used in the construction of the scanset.
A range of characters may be represented by the construct
first\-last ,
thus [0123456789] may be expressed [0\-9].
.sp
Using this convention,
first
must be lexically less than or equal to
last ,
or else the dash stands for itself. The dash also stands for
itself whenever it is the first or the last character in the scanset.
To include the right square bracket as an element of the scanset,
it must appear as the first character (possibly preceded by a
circumflex) of the scanset, and in this case it is not
syntactically interpreted as the closing bracket.
The corresponding argument must point to a character array large
enough to hold the data field and the terminating \fB\\0\fR, which is
added automatically.
At least one character must match
for this conversion to be considered successful.
.PP
The conversion characters \fId, u, o,\fP and \fIx\fP may be capitalized or preceded by \fIl\fP or \fIh\fP to indicate that a pointer to long or to short, rather than to int,
is in the argument list. Similarly, the conversion characters \fIe, f,\fP and \fIg\fP may be capitalized or preceded by \fIl\fP to indicate that a pointer to double, rather than to float,
is in the argument list.
The "l" or "h" modifier is ignored for other conversion characters.
.PP
The \fBSCANF\fP subroutine conversion terminates at EOF, at the end of the control string,
or when an input character conflicts with the control string.
In the latter case, the offending character is left unread in the input stream.
.PP
The \fBSCANF\fP subroutine returns the number of successfully matched
and assigned input items. This number can be zero
in the event of an early conflict between an input
character and the control string.
If the input ends before the first conflict or conversion, EOF is returned.
.ed
.sh EXAMPLE
The call:
.bb
int i, n; float x; char name[50];
.sp
n = scanf("%d%f%s", &i, &x, name);
.eb
with the input line: 25 54.32E\-1 thompson
.sp
assigns to n the value 3, to i the value 25, to x the value 5.432,
and name will contain thompson.
.sp
Or:
.sp
.bb
int i; float x; char name[50];
.sp
scanf("%2d%f%\ %[0\-9]", &i, &x, name);
.eb
with input: 56789 0123 56a72 will assign 56 to i, 789.0 to x,
skip 0123, and place the string 560 in \fIname.\fP
.eb
.db
The next call to \fIgetchar()\fP will return \fIa.\fP For further information, see 
getc.
.ed
.sh CAVEATS
.db
The success of literal matches and suppressed
assignments is not directly determinable.
.PP
Trailing white space (including a new-line)
is left unread unless matched in the control string.
.ed
.sh DIAGNOSTICS
.db
These functions return EOF on end of input and a short count for missing
or illegal data items.
.ed
.sh SEE ALSO
.bb
atof(), getc(), printf()
.eb
