xref: /openbsd-src/lib/libc/stdio/scanf.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: scanf.3,v 1.10 2000/12/24 00:30:59 aaron 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. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.Dd January 31, 1995
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 read input according to the given
68.Fa format
69as described below.
70This format may contain
71.Dq conversion specifiers ;
72the results of such conversions, if any, are stored through a set of pointer
73arguments.
74.Pp
75The
76.Fn scanf
77function reads input from the standard input stream
78.Em stdin ,
79.Fn fscanf
80reads input from the supplied stream pointer
81.Fa stream ,
82and
83.Fn sscanf
84reads its input from the character string pointed to by
85.Fa str .
86.Pp
87The
88.Fn vfscanf
89function is analogous to
90.Xr vfprintf 3
91and reads input from the stream pointer
92.Fa stream
93using a variable argument list of pointers (see
94.Xr stdarg 3 ) .
95The
96.Fn vscanf
97function scans a variable argument list from the standard input and the
98.Fn vsscanf
99function scans it from a string; these are analogous to the
100.Fn vprintf
101and
102.Fn vsprintf
103functions, respectively.
104.Pp
105Each successive
106.Em pointer
107argument must correspond properly with each successive conversion specifier
108(but see
109.Dq suppression
110below).
111All conversions are introduced by the
112.Cm %
113(percent sign) character.
114The
115.Fa format
116string may also contain other characters.
117Whitespace (such as blanks, tabs, or newlines) in the
118.Fa format
119string match any amount of whitespace, including none, in the input.
120Everything else matches only itself.
121Scanning stops when an input character does not match such a format character.
122Scanning also stops when an input conversion cannot be made (see below).
123.Sh CONVERSIONS
124Following the
125.Cm %
126character introducing a conversion there may be a number of
127.Em flag
128characters, as follows:
129.Bl -tag -width indent
130.It Cm *
131Suppresses assignment.
132The conversion that follows occurs as usual, but no pointer is used;
133the result of the conversion is simply discarded.
134.It Cm h
135Indicates that the conversion will be one of
136.Cm dioux
137or
138.Cm n
139and the next pointer is a pointer to a
140.Li short int
141(rather than
142.Li int ) .
143.It Cm l
144Indicates either that the conversion will be one of
145.Cm dioux
146or
147.Cm n
148and the next pointer is a pointer to a
149.Li long int
150(rather than
151.Li int ) ,
152or that the conversion will be one of
153.Cm efg
154and the next pointer is a pointer to
155.Li double
156(rather than
157.Li float ) .
158.It Cm q
159Indicates that the conversion will be one of
160.Cm dioux
161or
162.Cm n
163and the next pointer is a pointer to a
164.Li quad_t
165(rather than
166.Li int ) .
167.It Cm L
168Indicates that the conversion will be
169.Cm efg
170and the next pointer is a pointer to
171.Li long double .
172.El
173.Pp
174In addition to these flags, there may be an optional maximum field width,
175expressed as a decimal integer, between the
176.Cm %
177and the conversion.
178If no width is given,
179a default of
180.Dq infinity
181is used (with one exception, below);
182otherwise at most this many characters are scanned in processing the
183conversion.
184Before conversion begins, most conversions skip whitespace;
185this whitespace is not counted against the field width.
186.Pp
187The following conversions are available:
188.Bl -tag -width XXXX
189.It Cm %
190Matches a literal `%'.
191That is,
192.Ql %\&%
193in the format string matches a single input
194.Ql %
195character.
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.Li int .
201.It Cm D
202Equivalent to
203.Cm 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.Li 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.Li unsigned int .
222.It Cm O
223Equivalent to
224.Cm 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.Li unsigned int .
230.It Cm x
231Matches an optionally signed hexadecimal integer;
232the next pointer must be a pointer to
233.Li unsigned int .
234.It Cm X
235Equivalent to
236.Cm x .
237.It Cm f
238Matches an optionally signed floating-point number;
239the next pointer must be a pointer to
240.Li float .
241.It Cm e
242Equivalent to
243.Cm f .
244.It Cm g
245Equivalent to
246.Cm f .
247.It Cm E
248Equivalent to
249.Cm f .
250.It Cm G
251Equivalent to
252.Cm f .
253.It Cm s
254Matches a sequence of non-whitespace characters;
255the next pointer must be a pointer to
256.Li char ,
257and the array must be large enough to accept all the sequence and the
258terminating
259.Tn NUL
260character.
261The input string stops at whitespace
262or at the maximum field width, whichever occurs first.
263.It Cm c
264Matches a sequence of
265.Li width
266count characters (default 1);
267the next pointer must be a pointer to
268.Li char ,
269and there must be enough room for all the characters
270(no terminating
271.Tn NUL
272is added).
273The usual skip of leading whitespace is suppressed.
274To skip whitespace first, use an explicit space in the format.
275.It Cm \&[
276Matches a nonempty sequence of characters from the specified set
277of accepted characters;
278the next pointer must be a pointer to
279.Li char ,
280and there must be enough room for all the characters in the string,
281plus a terminating
282.Tn NUL
283character.
284The usual skip of leading whitespace is suppressed.
285The string is to be made up of characters in
286(or not in)
287a particular set;
288the set is defined by the characters between the open bracket
289.Cm [
290character
291and a close bracket
292.Cm ]
293character.
294The set excludes those characters
295if the first character after the open bracket is a circumflex
296.Cm ^ .
297To include a close bracket in the set,
298make it the first character after the open bracket
299or the circumflex;
300any other position will end the set.
301The hyphen character
302.Cm -
303is also special;
304when placed between two other characters,
305it adds all intervening characters to the set.
306To include a hyphen,
307make it the last character before the final close bracket.
308For instance,
309.Ql [^]0-9-]
310means the set `everything except close bracket, zero through nine,
311and hyphen'.
312The string ends with the appearance of a character not in the
313(or, with a circumflex, in) set
314or when the field width runs out.
315.It Cm p
316Matches a pointer value (as printed by
317.Ql %p
318in
319.Xr printf 3 ) ;
320the next pointer must be a pointer to
321.Li void .
322.It Cm n
323Nothing is expected;
324instead, the number of characters consumed thus far from the input
325is stored through the next pointer,
326which must be a pointer to
327.Li int .
328This is
329.Em not
330a conversion, although it can be suppressed with the
331.Cm *
332flag.
333.El
334.Pp
335For backwards compatibility, other conversion characters (except
336.Ql \e0 )
337are taken as if they were
338.Ql %d
339or, if uppercase,
340.Ql %ld ,
341and a `conversion' of
342.Ql %\e0
343causes an immediate return of
344.Dv EOF .
345.Sh RETURN VALUES
346These functions return the number of input items assigned, which can be fewer
347than provided for, or even zero, in the event of a matching failure.
348Zero indicates that, while there was input available, no conversions were
349assigned; typically this is due to an invalid input character,
350such as an alphabetic character for a
351.Ql %d
352conversion.
353The value
354.Dv EOF
355is returned if an input failure occurs before any conversion such as an
356end-of-file occurs.
357If an error or end-of-file occurs after conversion has begun,
358the number of conversions which were successfully completed is returned.
359.Sh SEE ALSO
360.Xr getc 3 ,
361.Xr printf 3 ,
362.Xr strtod 3 ,
363.Xr strtol 3 ,
364.Xr strtoul 3
365.Sh STANDARDS
366The functions
367.Fn fscanf ,
368.Fn scanf ,
369and
370.Fn sscanf
371conform to
372.St -ansiC .
373.Sh HISTORY
374The functions
375.Fn vscanf ,
376.Fn vsscanf ,
377and
378.Fn vfscanf
379first appeared in
380.Bx 4.3 Reno .
381.Sh BUGS
382All of the backwards compatibility formats will be removed in the future.
383.Pp
384Numerical strings are truncated to 512 characters; for example,
385.Cm %f
386and
387.Cm %d
388are implicitly
389.Cm %512f
390and
391.Cm %512d .
392