scanf(3S) scanf(3S)
NAME
scanf, fscanf, sscanf - convert formatted input
SYNOPSIS
#include <stdio.h>
int scanf (format [ , pointer ] ... )
char *format;
int fscanf (stream, format [ , pointer ] ... )
FILE *stream;
char *format;
int sscanf (s, format [ , pointer ] ... )
char *s, *format;
DESCRIPTION
scanf reads from the standard input stream stdin. Fscanf
reads from the named input stream. Sscanf reads from the
character string s. Each function reads characters, inter-
prets them according to a format, and stores the results in
its arguments. Each expects, as arguments, a control string
format described below, and a set of pointer arguments indi-
cating where the converted input should be stored. The
results are undefined in there are insufficient args for the
format. If the format is exhausted while args remain, the
excess args are simply ignored.
The control string usually contains conversion specifica-
tions, which are used to direct interpretation of input
sequences. The control string may contain:
1. White-space characters (blanks, tabs, new-lines, or
form-feeds) which, except in two cases described below,
cause input to be read up to the next non-white-space
character.
2. An ordinary character (not %), which must match the next
character of the input stream.
3. Conversion specifications, consisting of the character %
or the character sequence %digits$, an optional assign-
ment suppressing character *, an optional numerical max-
imum field width, an optional h, l (ell), if L indicating
the size of the receiving variable, and a conversion
code.
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 assign-
ment 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
Page 1 CX/UX Programmer's Reference Manual
scanf(3S) scanf(3S)
character or until the field width, if specified, is
exhausted. For all descriptors except ``['' and ``c'',
white space leading an input field is ignored.
Conversions can be applied to the nth argument in the argu-
ment list, rather than to the next unused argument. In this
case, the conversion character % (see above) is replaced by
the sequence %digits$, where digits is a decimal integer n,
giving the position of the argument in the argument list.
The first such argument, %1$, immediately follows format.
The control string can contain either form of a conversion
specification, i.e., % or %digits$, although the two forms
cannot be mixed within a single control string.
The conversion code indicates the interpretation of the
input field; the corresponding pointer argument must usually
be of a restricted type. For a suppressed field, no pointer
argument is given. The following conversion codes are
legal:
% a single % is expected in the input at this point; no
assignment is done.
d a decimal integer is expected; the corresponding argu-
ment should be an integer pointer.
u an unsigned decimal integer is expected; the
corresponding argument should be an unsigned integer
pointer.
o an octal integer is expected; the corresponding argu-
ment should be an integer pointer.
x a hexadecimal integer is expected; the corresponding
argument should be an integer pointer. If the hexade-
cimal integer begins with 0x or 0X, the default action
is to input the number zero with the succeeding x or X
delimiting the beginning of the field for the next
conversion code. However, in programs which are linked
in one of the ANSI C compilation modes (see hc(1)), and
in programs which are linked in 88open OCS-compliant
mode, will regard the 0x or 0X as a prefix to the
actual number and input the number following the x or
X.
i an integer is expected; the corresponding argument
should be an integer pointer. It will store the value
of the next input item interpreted according to C con-
ventions: a leading ``0'' implies octal; a leading
``0x'' implies hexadecimal; otherwise, decimal.
p Matches an implementation-defined set of sequences,
Page 2 CX/UX Programmer's Reference Manual
scanf(3S) scanf(3S)
which should be the same as the set of sequences that
may be produced by the %p conversion of the printf
function. The corresponding argument should be a
pointer to void. The interpretation of the input item
is implementation-defined. If the input item is a value
converted earlier during the same program execution,
the pointer that results shall compare equal to that
value; otherwise, the behavior of the %p conversion is
undefined.
n stores in an integer argument the total number of char-
acters (including white space) that have been scanned
so far since the function call. No input is consumed.
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 con-
taining a decimal point, followed by an optional
exponent field consisting of an E or an e, followed by
an optional +, -, or space, followed by an integer.
s a character string is expected; the corresponding argu-
ment 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 automatically. The
input field is terminated by a white-space character.
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.
[ indicates string data and the normal skip over leading
white space is suppressed. The left bracket is fol-
lowed by a set of characters, which we will call 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].
Using this convention, first must be lexically less
Page 3 CX/UX Programmer's Reference Manual
scanf(3S) scanf(3S)
than or equal to last, or else the dash will stand for
itself. The dash will also stand 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 will not be 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 \0, which will be
added automatically. At least one character must match
for this conversion to be considered successful.
The conversion characters d, u, o, x and i may be preceded
by l or h to indicate that a pointer to long or to short
rather than to int is in the argument list. Similarly, the
conversion characters e, f, and g may be preceded by l to
indicate that the corresponding argument is a pointer to a
double rather than a pointer to a float, or by a L if it is
a pointer to a long double. When the conversion characters
E, F, and G are used, the default practice is to assume that
a pointer to double is in the argument list. However, in
programs which are linked in one of the ANSI C compilation
modes (see hc(1)), and in programs which are linked in
88open OCS-compliant mode, the E, F, and G conversion char-
acters behave the same as, respectively, e, f, and g. The
h, l, or L modifier is ignored for other conversion charac-
ters.
scanf conversion terminates at EOF, at the end of the con-
trol 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.
scanf 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 con-
trol string. If the input ends before the first conflict or
conversion, EOF is returned.
EXAMPLES
The call:
int n, i ; float x; char name[50];
n = scanf("%d%f%s", &i, &x, name);
with the input line:
25 54.32E-1 thompson
will assign to n the value 3, to i the value 25, to x the
value 5.432, and name will contain thompson\0 . Or:
Page 4 CX/UX Programmer's Reference Manual
scanf(3S) scanf(3S)
int i, j; float x; char name[50];
(void) scanf("%i%2d%f%*d %[0-9] ", &j, &i, &x, name);
with input:
011 56789 0123 56a72
will assign 9 to j, 56 to i, 789.0 to x, skip 0123, and
place the string 56\0 in name. The next call to getchar
[see getc(3S)] will return a. Or:
int i, j, s, e; char name[50];
(void) scanf("%i %i %n%s%n", &i, &j, &s, name, &e);
with input:
0x11 0xy johnson
will assign 17 to i, 0 to j, 6 to s, will place the string
xy\0 in name, and will assign 8 to e. Thus, the length of
name is e - s = 2 . The next call to getchar [see getc(3S)]
will return a blank.
SEE ALSO
hc(1), getc(3S), printf(3S), stdio(3S), strtod(3C),
strtol(3C).
DIAGNOSTICS
These functions return EOF on end of input and a short count
for missing or illegal data items.
CAVEATS
Trailing white space (including a new-line) is left unread
unless matched in the control string.
Page 5 CX/UX Programmer's Reference Manual