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