xref: /386bsd/usr/src/lib/libc/stdio/scanf.3 (revision a2142627)
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 and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     @(#)scanf.3	6.14 (Berkeley) 1/8/93
37.\"
38.Dd January 8, 1993
39.Dt SCANF 3
40.Os
41.Sh NAME
42.Nm scanf ,
43.Nm fscanf ,
44.Nm sscanf ,
45.Nm vscanf ,
46.Nm vsscanf ,
47.Nm vfscanf
48.Nd input format conversion
49.Sh SYNOPSIS
50.Fd #include <stdio.h>
51.Ft int
52.Fn scanf "const char *format" ...
53.Ft int
54.Fn fscanf "FILE *stream" "const char *format" ...
55.Ft int
56.Fn sscanf "const char *str" "const char *format" ...
57.Fd #include <stdarg.h>
58.Ft int
59.Fn vscanf "const char *format" "va_list ap"
60.Ft int
61.Fn vsscanf "const char *str" "const char *format" "va_list ap"
62.Ft int
63.Fn vfscanf "FILE *stream" "const char *format" "va_list ap"
64.Sh DESCRIPTION
65The
66.Fn scanf
67family of functions scans input according to a
68.Fa format
69as described below.
70This format may contain
71.Em conversion specifiers ;
72the results from such conversions, if any,
73are stored through the
74.Em pointer
75arguments.
76The
77.Fn scanf
78function
79reads input from the standard input stream
80.Em stdin ,
81.Fn fscanf
82reads input from the stream pointer
83.Fa stream ,
84and
85.Fn sscanf
86reads its input from the character string pointed to by
87.Fa str .
88The
89.Fn vfscanf
90function
91is analogous to
92.Xr vfprintf 3
93and reads input from the stream pointer
94.Fa stream
95using a variable argument list of pointers (see
96.Xr stdarg 3 ) .
97The
98.Fn vscanf
99function scans a variable argument list from the standard input and
100the
101.Fn vsscanf
102function scans it from a string;
103these are analogous to
104the
105.Fn vprintf
106and
107.Fn vsprintf
108functions respectively.
109Each successive
110.Em pointer
111argument must correspond properly with
112each successive conversion specifier
113(but see `suppression' below).
114All conversions are introduced by the
115.Cm %
116(percent sign) character.
117The
118.Fa format
119string
120may also contain other characters.
121White space (such as blanks, tabs, or newlines) in the
122.Fa format
123string match any amount of white space, including none, in the input.
124Everything else
125matches only itself.
126Scanning stops
127when an input character does not match such a format character.
128Scanning also stops
129when an input conversion cannot be made (see below).
130.Sh CONVERSIONS
131Following the
132.Cm %
133character introducing a conversion
134there may be a number of
135.Em flag
136characters, as follows:
137.Bl -tag -width indent
138.It Cm *
139Suppresses assignment.
140The conversion that follows occurs as usual, but no pointer is used;
141the result of the conversion is simply discarded.
142.It Cm h
143Indicates that the conversion will be one of
144.Cm dioux
145or
146.Cm n
147and the next pointer is a pointer to a
148.Em short  int
149(rather than
150.Em int ) .
151.It Cm l
152Indicates either that the conversion will be one of
153.Cm dioux
154or
155.Cm n
156and the next pointer is a pointer to a
157.Em long  int
158(rather than
159.Em int ) ,
160or that the conversion will be one of
161.Cm efg
162and the next pointer is a pointer to
163.Em double
164(rather than
165.Em float ) .
166.It Cm L
167Indicates that the conversion will be
168.Cm efg
169and the next pointer is a pointer to
170.Em long double .
171(This type is not implemented; the
172.Cm L
173flag is currently ignored.)
174.El
175.Pp
176In addition to these flags,
177there may be an optional maximum field width,
178expressed as a decimal integer,
179between the
180.Cm %
181and the conversion.
182If no width is given,
183a default of `infinity' is used (with one exception, below);
184otherwise at most this many characters are scanned
185in processing the conversion.
186Before conversion begins,
187most conversions skip white space;
188this white space is not counted against the field width.
189.Pp
190The following conversions are available:
191.Bl -tag -width XXXX
192.It Cm %
193Matches a literal `%'.
194That is, `%\&%' in the format string
195matches a single input `%' character.
196No conversion is done, and assignment does not occur.
197.It Cm d
198Matches an optionally signed decimal integer;
199the next pointer must be a pointer to
200.Em int .
201.It Cm D
202Equivalent to
203.Xr ld ;
204this exists only for backwards compatibility.
205.It Cm i
206Matches an optionally signed integer;
207the next pointer must be a pointer to
208.Em int .
209The integer is read in base 16 if it begins
210with
211.Ql 0x
212or
213.Ql 0X ,
214in base 8 if it begins with
215.Ql 0 ,
216and in base 10 otherwise.
217Only characters that correspond to the base are used.
218.It Cm o
219Matches an octal integer;
220the next pointer must be a pointer to
221.Em unsigned int .
222.It Cm O
223Equivalent to
224.Xr lo ;
225this exists for backwards compatibility.
226.It Cm u
227Matches an optionally signed decimal integer;
228the next pointer must be a pointer to
229.Em unsigned int .
230.It Cm x
231Matches an optionally a signed hexadecimal integer;
232the next pointer must be a pointer to
233.Em unsigned int .
234.It Cm X
235Equivalent to
236.Cm lx ;
237this violates the
238.St -ansiC ,
239but is backwards compatible with previous
240.Ux
241systems.
242.It Cm f
243Matches an optionally signed floating-point number;
244the next pointer must be a pointer to
245.Em float .
246.It Cm e
247Equivalent to
248.Cm f .
249.It Cm g
250Equivalent to
251.Cm f .
252.It Cm E
253Equivalent to
254.Cm lf ;
255this violates the
256.St -ansiC ,
257but is backwards compatible with previous
258.Ux
259systems.
260.It Cm F
261Equivalent to
262.Cm lf ;
263this exists only for backwards compatibility.
264.It Cm s
265Matches a sequence of non-white-space characters;
266the next pointer must be a pointer to
267.Em char ,
268and the array must be large enough to accept all the sequence and the
269terminating
270.Dv NUL
271character.
272The input string stops at white space
273or at the maximum field width, whichever occurs first.
274.It Cm c
275Matches a sequence of
276.Em width
277count
278characters (default 1);
279the next pointer must be a pointer to
280.Em char ,
281and there must be enough room for all the characters
282(no terminating
283.Dv NUL
284is added).
285The usual skip of leading white space is suppressed.
286To skip white space first, use an explicit space in the format.
287.It Cm \&[
288Matches a nonempty sequence of characters from the specified set
289of accepted characters;
290the next pointer must be a pointer to
291.Em char ,
292and there must be enough room for all the characters in the string,
293plus a terminating
294.Dv NUL
295character.
296The usual skip of leading white space is suppressed.
297The string is to be made up of characters in
298(or not in)
299a particular set;
300the set is defined by the characters between the open bracket
301.Cm [
302character
303and a close bracket
304.Cm ]
305character.
306The set
307.Em excludes
308those characters
309if the first character after the open bracket is a circumflex
310.Cm ^ .
311To include a close bracket in the set,
312make it the first character after the open bracket
313or the circumflex;
314any other position will end the set.
315The hyphen character
316.Cm -
317is also special;
318when placed between two other characters,
319it adds all intervening characters to the set.
320To include a hyphen,
321make it the last character before the final close bracket.
322For instance,
323.Ql [^]0-9-]
324means the set `everything except close bracket, zero through nine,
325and hyphen'.
326The string ends with the appearance of a character not in the
327(or, with a circumflex, in) set
328or when the field width runs out.
329.It Cm p
330Matches a pointer value (as printed by
331.Ql %p
332in
333.Xr printf 3 ) ;
334the next pointer must be a pointer to
335.Em void .
336.It Cm n
337Nothing is expected;
338instead, the number of characters consumed thus far from the input
339is stored through the next pointer,
340which must be a pointer to
341.Em int .
342This is
343.Em not
344a conversion, although it can be suppressed with the
345.Cm *
346flag.
347.El
348.Pp
349For backwards compatibility,
350other conversion characters (except
351.Ql \e0 )
352are taken as if they were
353.Ql %d
354or, if uppercase,
355.Ql %ld ,
356and a `conversion' of
357.Ql %\e0
358causes an immediate return of
359.Dv EOF .
360The
361.Cm F
362and
363.Cm X
364conversions will be changed in the future
365to conform to the
366.Tn ANSI
367C standard,
368after which they will act like
369.Cm f
370and
371.Cm x
372respectively.
373.Pp
374.Sh RETURN VALUES
375These
376functions
377return
378the number of input items assigned, which can be fewer than provided
379for, or even zero, in the event of a matching failure.
380Zero
381indicates that, while there was input available,
382no conversions were assigned;
383typically this is due to an invalid input character,
384such as an alphabetic character for a
385.Ql %d
386conversion.
387The value
388.Dv EOF
389is returned if an input failure occurs before any conversion such as an
390end-of-file occurs. If an error or end-of-file occurs after conversion
391has begun,
392the number of conversions which were successfully completed is returned.
393.Sh SEE ALSO
394.Xr strtol 3 ,
395.Xr strtoul 3 ,
396.Xr strtod 3 ,
397.Xr getc 3 ,
398.Xr printf 3
399.Sh STANDARDS
400The functions
401.Fn fscanf ,
402.Fn scanf ,
403and
404.Fn sscanf
405conform to
406.St -ansiC .
407.Sh HISTORY
408The functions
409.Fn vscanf ,
410.Fn vsscanf
411and
412.Fn vfscanf
413are new to this release.
414.Sh BUGS
415The current situation with
416.Cm %F
417and
418.Cm %X
419conversions is unfortunate.
420.Pp
421All of the backwards compatibility formats will be removed in the future.
422.Pp
423Numerical strings are truncated to 512 characters; for example,
424.Cm %f
425and
426.Cm %d
427are implicitly
428.Cm %512f
429and
430.Cm %512d .
431