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