xref: /freebsd/lib/libc/stdio/scanf.3 (revision 2aebedc3ad9e722b272254e6dd3a12e399595e57)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  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	8.2 (Berkeley) 12/11/93
37.\"
38.Dd December 11, 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.It Cm q
175Indicates either that the conversion will be one of
176.Cm dioux
177or
178.Cm n
179and the next pointer is a pointer to a
180.Em long long int
181(rather than
182.Em int ) ,
183.El
184.Pp
185In addition to these flags,
186there may be an optional maximum field width,
187expressed as a decimal integer,
188between the
189.Cm %
190and the conversion.
191If no width is given,
192a default of `infinity' is used (with one exception, below);
193otherwise at most this many characters are scanned
194in processing the conversion.
195Before conversion begins,
196most conversions skip white space;
197this white space is not counted against the field width.
198.Pp
199The following conversions are available:
200.Bl -tag -width XXXX
201.It Cm %
202Matches a literal `%'.
203That is, `%\&%' in the format string
204matches a single input `%' character.
205No conversion is done, and assignment does not occur.
206.It Cm d
207Matches an optionally signed decimal integer;
208the next pointer must be a pointer to
209.Em int .
210.It Cm D
211Equivalent to
212.Cm ld ;
213this exists only for backwards compatibility.
214.It Cm i
215Matches an optionally signed integer;
216the next pointer must be a pointer to
217.Em int .
218The integer is read in base 16 if it begins
219with
220.Ql 0x
221or
222.Ql 0X ,
223in base 8 if it begins with
224.Ql 0 ,
225and in base 10 otherwise.
226Only characters that correspond to the base are used.
227.It Cm o
228Matches an octal integer;
229the next pointer must be a pointer to
230.Em unsigned int .
231.It Cm O
232Equivalent to
233.Cm lo ;
234this exists for backwards compatibility.
235.It Cm u
236Matches an optionally signed decimal integer;
237the next pointer must be a pointer to
238.Em unsigned int .
239.It Cm x
240Matches an optionally signed hexadecimal integer;
241the next pointer must be a pointer to
242.Em unsigned int .
243.It Cm X
244Equivalent to
245.Cm lx ;
246this violates the
247.St -ansiC ,
248but is backwards compatible with previous
249.Ux
250systems.
251.It Cm f
252Matches an optionally signed floating-point number;
253the next pointer must be a pointer to
254.Em float .
255.It Cm e
256Equivalent to
257.Cm f .
258.It Cm g
259Equivalent to
260.Cm f .
261.It Cm E
262Equivalent to
263.Cm lf ;
264this violates the
265.St -ansiC ,
266but is backwards compatible with previous
267.Ux
268systems.
269.It Cm F
270Equivalent to
271.Cm lf ;
272this exists only for backwards compatibility.
273.It Cm s
274Matches a sequence of non-white-space characters;
275the next pointer must be a pointer to
276.Em char ,
277and the array must be large enough to accept all the sequence and the
278terminating
279.Dv NUL
280character.
281The input string stops at white space
282or at the maximum field width, whichever occurs first.
283.It Cm c
284Matches a sequence of
285.Em width
286count
287characters (default 1);
288the next pointer must be a pointer to
289.Em char ,
290and there must be enough room for all the characters
291(no terminating
292.Dv NUL
293is added).
294The usual skip of leading white space is suppressed.
295To skip white space first, use an explicit space in the format.
296.It Cm \&[
297Matches a nonempty sequence of characters from the specified set
298of accepted characters;
299the next pointer must be a pointer to
300.Em char ,
301and there must be enough room for all the characters in the string,
302plus a terminating
303.Dv NUL
304character.
305The usual skip of leading white space is suppressed.
306The string is to be made up of characters in
307(or not in)
308a particular set;
309the set is defined by the characters between the open bracket
310.Cm [
311character
312and a close bracket
313.Cm ]
314character.
315The set
316.Em excludes
317those characters
318if the first character after the open bracket is a circumflex
319.Cm ^ .
320To include a close bracket in the set,
321make it the first character after the open bracket
322or the circumflex;
323any other position will end the set.
324The hyphen character
325.Cm -
326is also special;
327when placed between two other characters,
328it adds all intervening characters to the set.
329To include a hyphen,
330make it the last character before the final close bracket.
331For instance,
332.Ql [^]0-9-]
333means the set `everything except close bracket, zero through nine,
334and hyphen'.
335The string ends with the appearance of a character not in the
336(or, with a circumflex, in) set
337or when the field width runs out.
338.It Cm p
339Matches a pointer value (as printed by
340.Ql %p
341in
342.Xr printf 3 ) ;
343the next pointer must be a pointer to
344.Em void .
345.It Cm n
346Nothing is expected;
347instead, the number of characters consumed thus far from the input
348is stored through the next pointer,
349which must be a pointer to
350.Em int .
351This is
352.Em not
353a conversion, although it can be suppressed with the
354.Cm *
355flag.
356.El
357.Pp
358For backwards compatibility,
359other conversion characters (except
360.Ql \e0 )
361are taken as if they were
362.Ql %d
363or, if uppercase,
364.Ql %ld ,
365and a `conversion' of
366.Ql %\e0
367causes an immediate return of
368.Dv EOF .
369The
370.Cm F
371and
372.Cm X
373conversions will be changed in the future
374to conform to the
375.Tn ANSI
376C standard,
377after which they will act like
378.Cm f
379and
380.Cm x
381respectively.
382.Pp
383.Sh RETURN VALUES
384These
385functions
386return
387the number of input items assigned, which can be fewer than provided
388for, or even zero, in the event of a matching failure.
389Zero
390indicates that, while there was input available,
391no conversions were assigned;
392typically this is due to an invalid input character,
393such as an alphabetic character for a
394.Ql %d
395conversion.
396The value
397.Dv EOF
398is returned if an input failure occurs before any conversion such as an
399end-of-file occurs. If an error or end-of-file occurs after conversion
400has begun,
401the number of conversions which were successfully completed is returned.
402.Sh SEE ALSO
403.Xr getc 3 ,
404.Xr printf 3 ,
405.Xr strtod 3 ,
406.Xr strtol 3 ,
407.Xr strtoul 3
408.Sh STANDARDS
409The functions
410.Fn fscanf ,
411.Fn scanf ,
412and
413.Fn sscanf
414conform to
415.St -ansiC .
416.Sh HISTORY
417The functions
418.Fn vscanf ,
419.Fn vsscanf
420and
421.Fn vfscanf
422are new to this release.
423.Sh BUGS
424The current situation with
425.Cm %F
426and
427.Cm %X
428conversions is unfortunate.
429.Pp
430All of the backwards compatibility formats will be removed in the future.
431.Pp
432Numerical strings are truncated to 512 characters; for example,
433.Cm %f
434and
435.Cm %d
436are implicitly
437.Cm %512f
438and
439.Cm %512d .
440