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