]> git.saurik.com Git - apple/libc.git/blame - stdio/FreeBSD/scanf.3
Libc-1044.1.2.tar.gz
[apple/libc.git] / stdio / FreeBSD / scanf.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. 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.
5b2abdfb
A
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)scanf.3 8.2 (Berkeley) 12/11/93
1f2f436a 33.\" $FreeBSD: src/lib/libc/stdio/scanf.3,v 1.25 2007/01/09 00:28:07 imp Exp $
5b2abdfb 34.\"
9385eb3d 35.Dd January 4, 2003
5b2abdfb
A
36.Dt SCANF 3
37.Os
38.Sh NAME
5b2abdfb 39.Nm fscanf ,
ad3c9f2a 40.Nm scanf ,
5b2abdfb 41.Nm sscanf ,
ad3c9f2a 42.Nm vfscanf ,
5b2abdfb 43.Nm vscanf ,
ad3c9f2a 44.Nm vsscanf
5b2abdfb
A
45.Nd input format conversion
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In stdio.h
50.Ft int
ad3c9f2a
A
51.Fo fscanf
52.Fa "FILE *restrict stream"
53.Fa "const char *restrict format" ...
54.Fc
5b2abdfb 55.Ft int
ad3c9f2a
A
56.Fo scanf
57.Fa "const char *restrict format" ...
58.Fc
5b2abdfb 59.Ft int
ad3c9f2a
A
60.Fo sscanf
61.Fa "const char *restrict s"
62.Fa "const char *restrict format" ...
63.Fc
5b2abdfb 64.In stdarg.h
ad3c9f2a 65.In stdio.h
5b2abdfb 66.Ft int
ad3c9f2a
A
67.Fo vfscanf
68.Fa "FILE *restrict stream"
69.Fa "const char *restrict format"
70.Fa "va_list arg"
71.Fc
5b2abdfb 72.Ft int
ad3c9f2a
A
73.Fo vscanf
74.Fa "const char *restrict format"
75.Fa "va_list arg"
76.Fc
5b2abdfb 77.Ft int
ad3c9f2a
A
78.Fo vsscanf
79.Fa "const char *restrict s"
80.Fa "const char *restrict format"
81.Fa "va_list arg"
82.Fc
5b2abdfb
A
83.Sh DESCRIPTION
84The
85.Fn scanf
86family of functions scans input according to a
ad3c9f2a 87.Fa format ,
5b2abdfb
A
88as described below.
89This format may contain
90.Em conversion specifiers ;
91the results from such conversions, if any,
92are stored through the
93.Em pointer
94arguments.
95The
96.Fn scanf
97function
98reads input from the standard input stream
9385eb3d 99.Dv stdin ,
5b2abdfb
A
100.Fn fscanf
101reads input from the stream pointer
102.Fa stream ,
103and
104.Fn sscanf
105reads its input from the character string pointed to by
ad3c9f2a
A
106.Fa s .
107.Pp
5b2abdfb
A
108The
109.Fn vfscanf
110function
111is analogous to
112.Xr vfprintf 3
113and reads input from the stream pointer
114.Fa stream
115using a variable argument list of pointers (see
116.Xr stdarg 3 ) .
117The
118.Fn vscanf
119function scans a variable argument list from the standard input and
120the
121.Fn vsscanf
122function scans it from a string;
123these are analogous to
124the
125.Fn vprintf
126and
127.Fn vsprintf
ad3c9f2a
A
128functions, respectively.
129.Pp
5b2abdfb
A
130Each successive
131.Em pointer
132argument must correspond properly with
133each successive conversion specifier
9385eb3d
A
134(but see the
135.Cm *
136conversion below).
5b2abdfb
A
137All conversions are introduced by the
138.Cm %
139(percent sign) character.
140The
141.Fa format
142string
143may also contain other characters.
144White space (such as blanks, tabs, or newlines) in the
145.Fa format
146string match any amount of white space, including none, in the input.
147Everything else
148matches only itself.
149Scanning stops
150when an input character does not match such a format character.
151Scanning also stops
152when an input conversion cannot be made (see below).
ad3c9f2a
A
153.Pp
154Extended locale versions of these functions are documented in
155.Xr scanf_l 3 .
156See
157.Xr xlocale 3
158for more information.
5b2abdfb
A
159.Sh CONVERSIONS
160Following the
161.Cm %
ad3c9f2a 162character introducing a conversion,
5b2abdfb
A
163there may be a number of
164.Em flag
165characters, as follows:
9385eb3d 166.Bl -tag -width ".Cm l No (ell)"
5b2abdfb
A
167.It Cm *
168Suppresses assignment.
169The conversion that follows occurs as usual, but no pointer is used;
170the result of the conversion is simply discarded.
9385eb3d
A
171.It Cm hh
172Indicates that the conversion will be one of
173.Cm dioux
174or
175.Cm n
176and the next pointer is a pointer to a
177.Vt char
178(rather than
179.Vt int ) .
5b2abdfb
A
180.It Cm h
181Indicates that the conversion will be one of
182.Cm dioux
183or
184.Cm n
185and the next pointer is a pointer to a
9385eb3d 186.Vt "short int"
5b2abdfb 187(rather than
9385eb3d
A
188.Vt int ) .
189.It Cm l No (ell)
190Indicates that the conversion will be one of
5b2abdfb
A
191.Cm dioux
192or
193.Cm n
194and the next pointer is a pointer to a
9385eb3d 195.Vt "long int"
5b2abdfb 196(rather than
9385eb3d
A
197.Vt int ) ,
198that the conversion will be one of
199.Cm a , e , f ,
200or
201.Cm g
5b2abdfb 202and the next pointer is a pointer to
9385eb3d
A
203.Vt double
204(rather than
205.Vt float ) ,
206or that the conversion will be one of
207.Cm c ,
208.Cm s
209or
210.Cm \&[
211and the next pointer is a pointer to an array of
212.Vt wchar_t
213(rather than
214.Vt char ) .
215.It Cm ll No (ell ell)
216Indicates that the conversion will be one of
217.Cm dioux
218or
219.Cm n
220and the next pointer is a pointer to a
221.Vt "long long int"
5b2abdfb 222(rather than
9385eb3d 223.Vt int ) .
5b2abdfb 224.It Cm L
9385eb3d
A
225Indicates that the conversion will be one of
226.Cm a , e , f ,
227or
228.Cm g
5b2abdfb 229and the next pointer is a pointer to
9385eb3d
A
230.Vt "long double" .
231.It Cm j
232Indicates that the conversion will be one of
233.Cm dioux
234or
235.Cm n
236and the next pointer is a pointer to a
237.Vt intmax_t
238(rather than
239.Vt int ) .
240.It Cm t
241Indicates that the conversion will be one of
242.Cm dioux
243or
244.Cm n
245and the next pointer is a pointer to a
246.Vt ptrdiff_t
247(rather than
248.Vt int ) .
249.It Cm z
250Indicates that the conversion will be one of
251.Cm dioux
252or
253.Cm n
254and the next pointer is a pointer to a
255.Vt size_t
256(rather than
257.Vt int ) .
5b2abdfb 258.It Cm q
9385eb3d
A
259(deprecated.)
260Indicates that the conversion will be one of
5b2abdfb
A
261.Cm dioux
262or
263.Cm n
264and the next pointer is a pointer to a
9385eb3d 265.Vt "long long int"
5b2abdfb 266(rather than
9385eb3d 267.Vt int ) .
5b2abdfb
A
268.El
269.Pp
270In addition to these flags,
271there may be an optional maximum field width,
272expressed as a decimal integer,
273between the
274.Cm %
275and the conversion.
276If no width is given,
9385eb3d
A
277a default of
278.Dq infinity
279is used (with one exception, below);
280otherwise at most this many bytes are scanned
5b2abdfb 281in processing the conversion.
9385eb3d
A
282In the case of the
283.Cm lc ,
284.Cm ls
285and
286.Cm l[
287conversions, the field width specifies the maximum number
288of multibyte characters that will be scanned.
5b2abdfb
A
289Before conversion begins,
290most conversions skip white space;
291this white space is not counted against the field width.
292.Pp
293The following conversions are available:
294.Bl -tag -width XXXX
295.It Cm %
9385eb3d
A
296Matches a literal
297.Ql % .
298That is,
299.Dq Li %%
300in the format string
301matches a single input
302.Ql %
303character.
5b2abdfb
A
304No conversion is done, and assignment does not occur.
305.It Cm d
306Matches an optionally signed decimal integer;
307the next pointer must be a pointer to
9385eb3d 308.Vt int .
5b2abdfb
A
309.It Cm i
310Matches an optionally signed integer;
311the next pointer must be a pointer to
9385eb3d 312.Vt int .
5b2abdfb
A
313The integer is read in base 16 if it begins
314with
315.Ql 0x
316or
317.Ql 0X ,
318in base 8 if it begins with
319.Ql 0 ,
320and in base 10 otherwise.
321Only characters that correspond to the base are used.
322.It Cm o
323Matches an octal integer;
324the next pointer must be a pointer to
9385eb3d 325.Vt "unsigned int" .
5b2abdfb
A
326.It Cm u
327Matches an optionally signed decimal integer;
328the next pointer must be a pointer to
9385eb3d
A
329.Vt "unsigned int" .
330.It Cm x , X
5b2abdfb
A
331Matches an optionally signed hexadecimal integer;
332the next pointer must be a pointer to
9385eb3d
A
333.Vt "unsigned int" .
334.It Cm a , A , e , E , f , F , g , G
335Matches a floating-point number in the style of
336.Xr strtod 3 .
337The next pointer must be a pointer to
338.Vt float
339(unless
340.Cm l
341or
342.Cm L
343is specified.)
5b2abdfb
A
344.It Cm s
345Matches a sequence of non-white-space characters;
346the next pointer must be a pointer to
9385eb3d 347.Vt char ,
5b2abdfb
A
348and the array must be large enough to accept all the sequence and the
349terminating
350.Dv NUL
351character.
352The input string stops at white space
353or at the maximum field width, whichever occurs first.
9385eb3d
A
354.Pp
355If an
356.Cm l
357qualifier is present, the next pointer must be a pointer to
358.Vt wchar_t ,
359into which the input will be placed after conversion by
360.Xr mbrtowc 3 .
361.It Cm S
362The same as
363.Cm ls .
5b2abdfb
A
364.It Cm c
365Matches a sequence of
366.Em width
367count
368characters (default 1);
369the next pointer must be a pointer to
9385eb3d 370.Vt char ,
5b2abdfb
A
371and there must be enough room for all the characters
372(no terminating
373.Dv NUL
374is added).
375The usual skip of leading white space is suppressed.
376To skip white space first, use an explicit space in the format.
9385eb3d
A
377.Pp
378If an
379.Cm l
380qualifier is present, the next pointer must be a pointer to
381.Vt wchar_t ,
382into which the input will be placed after conversion by
383.Xr mbrtowc 3 .
384.It Cm C
385The same as
386.Cm lc .
5b2abdfb
A
387.It Cm \&[
388Matches a nonempty sequence of characters from the specified set
389of accepted characters;
390the next pointer must be a pointer to
9385eb3d 391.Vt char ,
5b2abdfb
A
392and there must be enough room for all the characters in the string,
393plus a terminating
394.Dv NUL
395character.
396The usual skip of leading white space is suppressed.
397The string is to be made up of characters in
398(or not in)
399a particular set;
400the set is defined by the characters between the open bracket
401.Cm [
402character
403and a close bracket
404.Cm ]
405character.
406The set
407.Em excludes
408those characters
409if the first character after the open bracket is a circumflex
410.Cm ^ .
411To include a close bracket in the set,
412make it the first character after the open bracket
413or the circumflex;
414any other position will end the set.
415The hyphen character
416.Cm -
417is also special;
418when placed between two other characters,
419it adds all intervening characters to the set.
420To include a hyphen,
421make it the last character before the final close bracket.
422For instance,
423.Ql [^]0-9-]
9385eb3d
A
424means the set
425.Dq "everything except close bracket, zero through nine, and hyphen" .
5b2abdfb
A
426The string ends with the appearance of a character not in the
427(or, with a circumflex, in) set
428or when the field width runs out.
9385eb3d
A
429.Pp
430If an
431.Cm l
432qualifier is present, the next pointer must be a pointer to
433.Vt wchar_t ,
434into which the input will be placed after conversion by
435.Xr mbrtowc 3 .
5b2abdfb
A
436.It Cm p
437Matches a pointer value (as printed by
438.Ql %p
439in
440.Xr printf 3 ) ;
441the next pointer must be a pointer to
ad3c9f2a
A
442.Vt "void *"
443(or other pointer type).
5b2abdfb
A
444.It Cm n
445Nothing is expected;
446instead, the number of characters consumed thus far from the input
447is stored through the next pointer,
448which must be a pointer to
9385eb3d 449.Vt int .
5b2abdfb
A
450This is
451.Em not
452a conversion, although it can be suppressed with the
453.Cm *
454flag.
455.El
456.Pp
457The decimal point
458character is defined in the program's locale (category
459.Dv LC_NUMERIC ) .
460.Pp
9385eb3d
A
461For backwards compatibility, a
462.Dq conversion
463of
5b2abdfb
A
464.Ql %\e0
465causes an immediate return of
466.Dv EOF .
5b2abdfb 467.Sh RETURN VALUES
ad3c9f2a
A
468These functions return the number of input items assigned.
469This can be fewer than provided for, or even zero,
470in the event of a matching failure.
471Zero indicates that, although there was input available,
5b2abdfb
A
472no conversions were assigned;
473typically this is due to an invalid input character,
474such as an alphabetic character for a
475.Ql %d
476conversion.
477The value
478.Dv EOF
479is returned if an input failure occurs before any conversion such as an
480end-of-file occurs.
481If an error or end-of-file occurs after conversion
482has begun,
483the number of conversions which were successfully completed is returned.
484.Sh SEE ALSO
485.Xr getc 3 ,
9385eb3d 486.Xr mbrtowc 3 ,
5b2abdfb 487.Xr printf 3 ,
ad3c9f2a 488.Xr scanf_l 3 ,
5b2abdfb
A
489.Xr strtod 3 ,
490.Xr strtol 3 ,
9385eb3d
A
491.Xr strtoul 3 ,
492.Xr wscanf 3
5b2abdfb
A
493.Sh STANDARDS
494The functions
495.Fn fscanf ,
496.Fn scanf ,
9385eb3d
A
497.Fn sscanf ,
498.Fn vfscanf ,
ad3c9f2a 499.Fn vscanf ,
5b2abdfb 500and
5b2abdfb 501.Fn vsscanf
9385eb3d
A
502conform to
503.St -isoC-99 .
5b2abdfb 504.Sh BUGS
9385eb3d
A
505Earlier implementations of
506.Nm
507treated
508.Cm \&%D , \&%E , \&%F , \&%O
5b2abdfb 509and
9385eb3d
A
510.Cm \&%X
511as their lowercase equivalents with an
512.Cm l
513modifier.
514In addition,
515.Nm
516treated an unknown conversion character as
517.Cm \&%d
518or
519.Cm \&%D ,
520depending on its case.
521This functionality has been removed.
5b2abdfb
A
522.Pp
523Numerical strings are truncated to 512 characters; for example,
524.Cm %f
525and
526.Cm %d
527are implicitly
528.Cm %512f
529and
530.Cm %512d .
9385eb3d
A
531.Pp
532The
533.Cm %n$
534modifiers for positional arguments are not implemented.
535.Pp
536The
537.Nm
538family of functions do not correctly handle multibyte characters in the
539.Fa format
540argument.