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