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