xref: /netbsd-src/lib/libc/stdio/scanf.3 (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1.\"	$NetBSD: scanf.3,v 1.20 2008/05/30 17:29:54 gdt Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)scanf.3	8.2 (Berkeley) 12/11/93
35.\"
36.Dd May 30, 2008
37.Dt SCANF 3
38.Os
39.Sh NAME
40.Nm scanf ,
41.Nm fscanf ,
42.Nm sscanf ,
43.Nm vscanf ,
44.Nm vsscanf ,
45.Nm vfscanf
46.Nd input format conversion
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In stdio.h
51.Ft int
52.Fn scanf "const char * restrict format" ...
53.Ft int
54.Fn fscanf "FILE * restrict stream" "const char * restrict format" ...
55.Ft int
56.Fn sscanf "const char * restrict str" "const char * restrict format" ...
57.In stdarg.h
58.Ft int
59.Fn vscanf "const char * restrict format" "va_list ap"
60.Ft int
61.Fn vsscanf "const char * restrict str" "const char * restrict format" "va_list ap"
62.Ft int
63.Fn vfscanf "FILE * restrict stream" "const char * restrict 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 hh
152Indicates 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 char
158(rather than
159.Em int ) .
160.It Cm j
161Indicates that the conversion will be one of
162.Cm dioux
163or
164.Cm n
165and the next pointer is a pointer to an
166.Em intmax_t
167(rather than
168.Em int ) .
169.It Cm l
170Indicates either that the conversion will be one of
171.Cm dioux
172or
173.Cm n
174and the next pointer is a pointer to a
175.Em long  int
176(rather than
177.Em int ) ,
178or that the conversion will be one of
179.Cm efg
180and the next pointer is a pointer to
181.Em double
182(rather than
183.Em float ) .
184.It Cm q
185Indicates that the conversion will be one of
186.Cm dioux
187or
188.Cm n
189and the next pointer is a pointer to a
190.Em quad_t
191(rather than
192.Em int ) .
193.It Cm t
194Indicates that the conversion will be one of
195.Cm dioux
196or
197.Cm n
198and the next pointer is a pointer to a
199.Em ptrdiff_t
200(rather than
201.Em int ) .
202.It Cm z
203Indicates that the conversion will be one of
204.Cm dioux
205or
206.Cm n
207and the next pointer is a pointer to a
208.Em size_t
209(rather than
210.Em int ) .
211.It Cm L
212Indicates that the conversion will be
213.Cm efg
214and the next pointer is a pointer to
215.Em long double .
216.El
217.Pp
218In addition to these flags,
219there may be an optional maximum field width,
220expressed as a decimal integer,
221between the
222.Cm %
223and the conversion.
224If no width is given,
225a default of `infinity' is used (with one exception, below);
226otherwise at most this many characters are scanned
227in processing the conversion.
228Before conversion begins,
229most conversions skip white space;
230this white space is not counted against the field width.
231.Pp
232The following conversions are available:
233.Bl -tag -width XXXX
234.It Cm %
235Matches a literal `%'.
236That is, `%\&%' in the format string
237matches a single input `%' character.
238No conversion is done, and assignment does not occur.
239.It Cm d
240Matches an optionally signed decimal integer;
241the next pointer must be a pointer to
242.Em int .
243.It Cm D
244Equivalent to
245.Cm ld ;
246this exists only for backwards compatibility.
247.It Cm i
248Matches an optionally signed integer;
249the next pointer must be a pointer to
250.Em int .
251The integer is read in base 16 if it begins
252with
253.Ql 0x
254or
255.Ql 0X ,
256in base 8 if it begins with
257.Ql 0 ,
258and in base 10 otherwise.
259Only characters that correspond to the base are used.
260.It Cm o
261Matches an octal integer;
262the next pointer must be a pointer to
263.Em unsigned int .
264.It Cm O
265Equivalent to
266.Cm lo ;
267this exists for backwards compatibility.
268.It Cm u
269Matches an optionally signed decimal integer;
270the next pointer must be a pointer to
271.Em unsigned int .
272.It Cm x
273Matches an optionally signed hexadecimal integer;
274the next pointer must be a pointer to
275.Em unsigned int .
276.It Cm X
277Equivalent to
278.Cm x .
279.It Cm f
280Matches an optionally signed floating-point number;
281the next pointer must be a pointer to
282.Em float .
283.It Cm e
284Equivalent to
285.Cm f .
286.It Cm g
287Equivalent to
288.Cm f .
289.It Cm E
290Equivalent to
291.Cm f .
292.It Cm G
293Equivalent to
294.Cm f .
295.It Cm s
296Matches a sequence of non-white-space characters;
297the next pointer must be a pointer to
298.Em char ,
299and the array must be large enough to accept all the sequence and the
300terminating
301.Dv NUL
302character.
303The input string stops at white space
304or at the maximum field width, whichever occurs first.
305.It Cm c
306Matches a sequence of
307.Em width
308count
309characters (default 1);
310the next pointer must be a pointer to
311.Em char ,
312and there must be enough room for all the characters
313(no terminating
314.Dv NUL
315is added).
316The usual skip of leading white space is suppressed.
317To skip white space first, use an explicit space in the format.
318.It Cm \&[
319Matches a nonempty sequence of characters from the specified set
320of accepted characters;
321the next pointer must be a pointer to
322.Em char ,
323and there must be enough room for all the characters in the string,
324plus a terminating
325.Dv NUL
326character.
327The usual skip of leading white space is suppressed.
328The string is to be made up of characters in
329(or not in)
330a particular set;
331the set is defined by the characters between the open bracket
332.Cm [
333character
334and a close bracket
335.Cm ]
336character.
337The set
338.Em excludes
339those characters
340if the first character after the open bracket is a circumflex
341.Cm ^ .
342To include a close bracket in the set,
343make it the first character after the open bracket
344or the circumflex;
345any other position will end the set.
346The hyphen character
347.Cm -
348is also special;
349when placed between two other characters,
350it adds all intervening characters to the set.
351To include a hyphen,
352make it the last character before the final close bracket.
353For instance,
354.Ql [^]0-9-]
355means the set `everything except close bracket, zero through nine,
356and hyphen'.
357The string ends with the appearance of a character not in the
358(or, with a circumflex, in) set
359or when the field width runs out.
360.It Cm p
361Matches a pointer value (as printed by
362.Ql %p
363in
364.Xr printf 3 ) ;
365the next pointer must be a pointer to
366.Em void .
367.It Cm n
368Nothing is expected;
369instead, the number of characters consumed thus far from the input
370is stored through the next pointer,
371which must be a pointer to
372.Em int .
373This is
374.Em not
375a conversion, although it can be suppressed with the
376.Cm *
377flag.
378.El
379.Pp
380For backwards compatibility,
381other conversion characters (except
382.Ql \e0 )
383are taken as if they were
384.Ql %d
385or, if uppercase,
386.Ql %ld ,
387and a `conversion' of
388.Ql %\e0
389causes an immediate return of
390.Dv EOF .
391.Sh RETURN VALUES
392These
393functions
394return
395the number of input items assigned, which can be fewer than provided
396for, or even zero, in the event of a matching failure.
397Zero
398indicates that, while there was input available,
399no conversions were assigned;
400typically this is due to an invalid input character,
401such as an alphabetic character for a
402.Ql %d
403conversion.
404The value
405.Dv EOF
406is returned if an input failure occurs before any conversion such as an
407end-of-file occurs.
408If an error or end-of-file occurs after conversion has begun,
409the number of conversions which were successfully completed is returned.
410.Sh SEE ALSO
411.Xr getc 3 ,
412.Xr printf 3 ,
413.Xr strtod 3 ,
414.Xr strtol 3 ,
415.Xr strtoul 3
416.Sh STANDARDS
417The functions
418.Fn fscanf ,
419.Fn scanf ,
420and
421.Fn sscanf
422conform to
423.St -isoC-90 .
424The
425.Cm %j ,
426.Cm %t
427and
428.Cm %z
429conversion format modifiers
430conform to
431.St -isoC-99 .
432The
433.Fn vfscanf ,
434.Fn vscanf
435and
436.Fn vsscanf
437functions conform to
438.St -isoC-99 .
439.Sh HISTORY
440The functions
441.Fn vscanf ,
442.Fn vsscanf
443and
444.Fn vfscanf
445appeared in
446.Bx 4.4
447or even
448.Bx 4.3 .
449.Sh NOTES
450All of the backwards compatibility formats will be removed in the future.
451.Sh BUGS
452Numerical strings are truncated to 512 characters; for example,
453.Cm %f
454and
455.Cm %d
456are implicitly
457.Cm %512f
458and
459.Cm %512d .
460