Make this i386-compatible; massive clean-up.
[unix-history] / usr / src / lib / libc / stdio / scanf.3
CommitLineData
858d320c
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
3abda555 3.\"
858d320c
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\"
7.\" %sccs.include.redist.man%
8.\"
571f1310 9.\" @(#)scanf.3 6.4 (Berkeley) %G%
858d320c
KB
10.\"
11.TH SCANF 3 ""
12.UC 7
3abda555 13.SH NAME
571f1310 14scanf, fscanf, sscanf, vfscanf \- formatted input conversion
3abda555
KM
15.SH SYNOPSIS
16.B #include <stdio.h>
17.PP
18.B scanf(format
858d320c 19[ , pointer ... ]
3abda555
KM
20.B )
21.br
22.B char *format;
23.PP
24.B fscanf(stream, format
858d320c 25[ , pointer ... ]
3abda555
KM
26.B )
27.br
28.SM
29.B FILE
30.B *stream;
31.br
32.B char *format;
33.PP
858d320c
KB
34.B sscanf(str, format
35[ , pointer ... ]
3abda555
KM
36.B )
37.br
571f1310
DS
38.B char *str;
39.br
40.B char *format;
41.PP
42.B #include <varargs.h>
43.PP
44.B vfscanf(stream, format, ap)
45.br
46.SM
47.B FILE
48.B *stream;
49.br
50.B char *format;
51.br
52.B va_list ap;
53.br
3abda555 54.SH DESCRIPTION
858d320c
KB
55The
56.I scanf
57family of functions scans input according to a
58.I format
59as described below.
60This format may contain
61.IR "conversion specifiers" ;
62the results from such conversions, if any,
63are stored through the
64.I pointer
65arguments.
3abda555 66.I Scanf
858d320c
KB
67reads the standard input stream
68.BR stdin ,
69.I fscanf
70reads the named input
71.IR stream ,
72and
73.I sscanf
3abda555 74reads from the character string
858d320c 75.IR str .
571f1310
DS
76.I Vfscanf
77is analogous to
78.I vfprintf
79and reads from a named input
80.I stream
81using a variable argument list of pointers (see
82.IR varargs (3)).
858d320c 83Each successive
3abda555 84.I pointer
858d320c
KB
85argument must correspond properly with
86each successive conversion specifier
87(but see `suppression' below).
88All conversions are introduced by the
89.B %
90(percent sign) character.
3abda555 91The
858d320c
KB
92.I format
93string
94may also contain other characters.
95White space (such as blanks, tabs, or newlines) in the
96.I format
97string match any amount of white space, including none, in the input.
98Everything else
99matches only itself.
100Scanning stops
101when an input character does not match such a format character.
102Scanning also stops
103when an input conversion cannot be made (see below).
104
105.SH CONVERSIONS
106Following the
107.B %
108character introducing a conversion
109there may be a number of
110.I flag
111characters, as follows:
3abda555 112.TP 4
858d320c
KB
113.B *
114suppresses assignment.
115The conversion that follows occurs as usual, but no pointer is used;
116the result of the conversion is simply discarded.
3abda555 117.TP 4
858d320c
KB
118.B h
119indicates that the conversion will be one of
120.B dioux
121or
122.B n
123and the next pointer is a pointer to a
124.B short int
125(rather than
126.BR int ).
3abda555 127.TP 4
858d320c
KB
128.B l
129indicates either that the conversion will be one of
130.B dioux
131or
132.B n
133and the next pointer is a pointer to a
134.B long int
135(rather than
136.BR int ),
137or that the conversion will be one of
138.B efg
139and the next pointer is a pointer to
140.B double
141(rather than
142.BR float ).
143.TP 4
144.B L
145indicates that the conversion will be
146.B efg
147and the next pointer is a pointer to
148.BR "long double" .
149(This type is not implemented; the
150.B L
151flag is currently ignored.)
3abda555 152.PP
858d320c
KB
153In addition to these flags,
154there may be an optional maximum field width,
155expressed as a decimal integer,
156between the
157.B %
158and the conversion.
159If no width is given,
160a default of `infinity' is used (with one exception, below);
161otherwise at most this many characters are scanned
162in processing the conversion.
163Before conversion begins,
164most conversions skip white space;
165this white space is not counted against the field width.
3abda555 166.PP
858d320c 167The following conversions are available:
3abda555 168.TP 4
858d320c
KB
169.B %
170matches a literal `%'.
171That is, `%%' in the format string
172matches a single input `%' character.
173No conversion is done, and no assignment occurs.
3abda555 174.TP 4
858d320c 175.B d
3abda555 176a decimal integer is expected;
858d320c
KB
177the next pointer must be a pointer to
178.BR int .
179.TP 4
180.B D
181equivalent to
182.BR ld ;
183this exists only for backwards compatibility.
184.TP 4
185.B i
186an integer is expected;
187the next pointer must be a pointer to
188.BR int .
189The integer is read in base 16 if it begins with `0x' or `0X',
190in base 8 if it begins with `0', and in base 10 otherwise.
191Only characters that correspond to the base are used.
3abda555 192.TP 4
858d320c 193.B o
3abda555 194an octal integer is expected;
858d320c
KB
195the next pointer must be a pointer to
196.BR "unsigned int" .
197.TP 4
198.B O
199equivalent to
200.BR lo ;
201this exists for backwards compatibility.
202.TP 4
203.B u
204a signed or unsigned decimal integer is expected;
205the next pointer must be a pointer to
206.BR "unsigned int" .
3abda555 207.TP 4
858d320c
KB
208.B x
209a signed or unsigned hexadecimal integer is expected;
210the next pointer must be a pointer to
211.BR "unsigned int" .
212.TP 4
213.B X
214equivalent to
215.BR lx ;
216this violates the ANSI C standard X3.159-1989,
217but is backwards compatible with previous
218.UX
219systems.
220.TP 4
221.B f
222a floating-point number is expected;
223the next pointer must be a pointer to
224.BR float .
225.TP 4
226.B e
227equivalent to
228.BR f .
3abda555 229.TP 4
858d320c
KB
230.B g
231equivalent to
232.BR f .
3abda555 233.TP 4
858d320c
KB
234.B E
235equivalent to
236.BR lf ;
237this violates the ANSI C standard X3.159-1989,
238but is backwards compatible with previous
239.UX
240systems.
3abda555 241.TP 4
858d320c
KB
242.B F
243equivalent to
244.BR lf ;
245this exists only for backwards compatibility.
3abda555 246.TP 4
858d320c
KB
247.B s
248a string is expected;
249the next pointer must be a pointer to
250.BR char ,
251and there must be enough room for all the characters in the string,
252plus a terminating `\e0'.
253The input string stops at white space
254or at the maximum field width, whichever occurs first.
255.TP 4
256.B c
257.I width
258characters (default 1) are expected;
259the next pointer must be a pointer to
260.BR char ,
261and there must be enough room for all the characters
262(no terminating `\e0' is added).
263The usual skip of leading white space is suppressed.
264To skip white space first, use an explicit space in the format.
265.TP 4
266.B [
267a string is expected;
268the next pointer must be a pointer to
269.BR char ,
270and there must be enough room for all the characters in the string,
271plus a terminating `\e0'.
272The usual skip of leading white space is suppressed.
273The string is to be made up of characters in
274(or not in)
275a particular set;
276the set is defined by the characters between the open bracket
277.B [
278character
279and a close bracket
280.B ]
281character.
282The set
283.I excludes
284those characters
285if the first character after the open bracket is a circumflex
286.BR ^ .
287To include a close bracket in the set,
288make it the first character after the open bracket
289or the circumflex;
290any other position will end the set.
291The hyphen character
292.B \-
293is also special;
294when placed between two other characters,
295it adds all intervening characters to the set.
296To include a hyphen,
297make it the last character before the final close bracket.
298For instance, `[^]0-9-]'
299means the set `everything except close bracket, zero through nine,
300and hyphen'.
301The string ends at the first character not in
302(or, with a circumflex, in)
303the set, or when the field width runs out.
304.TP 4
305.B p
306a pointer value (as printed by `%p' in
307.IR printf (3))
308is expected;
309the next pointer must be a pointer to
310.BR void .
311.TP 4
312.B n
313nothing is expected;
314instead, the number of characters consumed thus far from the input
315is stored through the next pointer,
316which must be a pointer to
317.BR int .
318This is
319.I not
320a conversion, although it can be suppressed with the
321.B *
322flag.
3abda555 323.PP
858d320c
KB
324For backwards compatibility,
325other conversion characters (except '\e0')
326are taken as if they were `%d' or, if uppercase, `%ld',
327and a `conversion' of `%\e0' causes an immediate return of
328.SM
329.BR EOF .
330The
331.B F
3abda555 332and
858d320c
KB
333.B X
334conversions will be changed in the future
335to conform to the ANSI C standard,
336after which they will act like
3abda555 337.B f
3abda555
KM
338and
339.B x
858d320c 340respectively.
3abda555
KM
341.PP
342The
343.I scanf
858d320c 344functions return the number of successfully assigned conversions, or
3abda555
KM
345.SM
346.B EOF
858d320c
KB
347if nothing was assigned
348before the end of input (or an error during input) was encountered.
349A return value of 0
350indicates that, while there was input available,
351no conversions were assigned;
352typically this is due to an invalid input character,
353such as an alphabetic character for a `%d' conversion.
3abda555 354.SH "SEE ALSO"
858d320c
KB
355strtol(3),
356strtoul(3),
86198885
KB
357getc(3),
358printf(3)
858d320c 359.SH "RETURN VALUE"
3abda555
KM
360The
361.I scanf
362functions return
363.SM
364.B EOF
365on end of input,
858d320c 366or a short count for missing or illegal data items.
3abda555 367.SH BUGS
858d320c
KB
368The current situation with
369.B %F
370and
371.B %X
372conversions is unfortunate.
373.br
374All of the backwards compatibility formats will be removed in the future.
571f1310
DS
375.br
376There is no
377.I vscanf
378or
379.IR vsscanf .
380.\" Had to draw the line somewhere!