]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/scanf.3
fuse.4: fuse_entry_out: rework discussion of uniqueness of nodeid + generation
[thirdparty/man-pages.git] / man3 / scanf.3
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 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
17 .\" 3. All advertising materials mentioning features or use of this software
18 .\" must display the following acknowledgement:
19 .\" This product includes software developed by the University of
20 .\" California, Berkeley and its contributors.
21 .\" 4. Neither the name of the University nor the names of its contributors
22 .\" may be used to endorse or promote products derived from this software
23 .\" without specific prior written permission.
24 .\"
25 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" SUCH DAMAGE.
36 .\" %%%LICENSE_END
37 .\"
38 .\" @(#)scanf.3 6.14 (Berkeley) 1/8/93
39 .\"
40 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
41 .\" modified to resemble the GNU libio setup used in the Linux libc
42 .\" used in versions 4.x (x>4) and 5 Helmut.Geyer@iwr.uni-heidelberg.de
43 .\" Modified, aeb, 970121
44 .\" 2005-07-14, mtk, added description of %n$ form; various text
45 .\" incorporated from the GNU C library documentation ((C) The
46 .\" Free Software Foundation); other parts substantially rewritten.
47 .\"
48 .\" 2008-06-23, mtk
49 .\" Add ERRORS section.
50 .\" Document the 'a' and 'm' modifiers for dynamic string allocation.
51 .\"
52 .TH SCANF 3 2016-03-15 "GNU" "Linux Programmer's Manual"
53 .SH NAME
54 scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf \- input format conversion
55 .SH SYNOPSIS
56 .nf
57 .B #include <stdio.h>
58
59 .BI "int scanf(const char *" format ", ...);"
60 .BI "int fscanf(FILE *" stream ", const char *" format ", ...);"
61 .BI "int sscanf(const char *" str ", const char *" format ", ...);"
62 .sp
63 .B #include <stdarg.h>
64
65 .BI "int vscanf(const char *" format ", va_list " ap );
66 .BI "int vsscanf(const char *" str ", const char *" format ", va_list " ap );
67 .BI "int vfscanf(FILE *" stream ", const char *" format ", va_list " ap );
68 .fi
69 .sp
70 .in -4n
71 Feature Test Macro Requirements for glibc (see
72 .BR feature_test_macros (7)):
73 .in
74 .ad l
75 .sp
76 .BR vscanf (),
77 .BR vsscanf (),
78 .BR vfscanf ():
79 .RS 4
80 _ISOC99_SOURCE || _POSIX_C_SOURCE\ >=\ 200112L
81 .ad
82 .RE
83 .SH DESCRIPTION
84 The
85 .BR scanf ()
86 family of functions scans input according to
87 .I format
88 as described below.
89 This format may contain
90 .IR "conversion specifications" ;
91 the results from such conversions, if any,
92 are stored in the locations pointed to by the
93 .I pointer
94 arguments that follow
95 .IR format .
96 Each
97 .I pointer
98 argument must be of a type that is appropriate for the value returned
99 by the corresponding conversion specification.
100
101 If the number of conversion specifications in
102 .I format
103 exceeds the number of
104 .I pointer
105 arguments, the results are undefined.
106 If the number of
107 .I pointer
108 arguments exceeds the number of conversion specifications, then the excess
109 .I pointer
110 arguments are evaluated, but are otherwise ignored.
111
112 The
113 .BR scanf ()
114 function reads input from the standard input stream
115 .IR stdin ,
116 .BR fscanf ()
117 reads input from the stream pointer
118 .IR stream ,
119 and
120 .BR sscanf ()
121 reads its input from the character string pointed to by
122 .IR str .
123 .PP
124 The
125 .BR vfscanf ()
126 function is analogous to
127 .BR vfprintf (3)
128 and reads input from the stream pointer
129 .I stream
130 using a variable argument list of pointers (see
131 .BR stdarg (3).
132 The
133 .BR vscanf ()
134 function scans a variable argument list from the standard input and the
135 .BR vsscanf ()
136 function scans it from a string; these are analogous to the
137 .BR vprintf (3)
138 and
139 .BR vsprintf (3)
140 functions respectively.
141 .PP
142 The
143 .I format
144 string consists of a sequence of
145 .I directives
146 which describe how to process the sequence of input characters.
147 If processing of a directive fails, no further input is read, and
148 .BR scanf ()
149 returns.
150 A "failure" can be either of the following:
151 .IR "input failure" ,
152 meaning that input characters were unavailable, or
153 .IR "matching failure" ,
154 meaning that the input was inappropriate (see below).
155
156 A directive is one of the following:
157 .TP
158 \(bu
159 A sequence of white-space characters (space, tab, newline, etc.; see
160 .BR isspace (3)).
161 This directive matches any amount of white space,
162 including none, in the input.
163 .TP
164 \(bu
165 An ordinary character (i.e., one other than white space or \(aq%\(aq).
166 This character must exactly match the next character of input.
167 .TP
168 \(bu
169 A conversion specification,
170 which commences with a \(aq%\(aq (percent) character.
171 A sequence of characters from the input is converted according to
172 this specification, and the result is placed in the corresponding
173 .I pointer
174 argument.
175 If the next item of input does not match the conversion specification,
176 the conversion fails\(emthis is a
177 .IR "matching failure" .
178 .PP
179 Each
180 .I conversion specification
181 in
182 .I format
183 begins with either the character \(aq%\(aq or the character sequence
184 "\fB%\fP\fIn\fP\fB$\fP"
185 (see below for the distinction) followed by:
186 .TP
187 \(bu
188 An optional \(aq*\(aq assignment-suppression character:
189 .BR scanf ()
190 reads input as directed by the conversion specification,
191 but discards the input.
192 No corresponding
193 .I pointer
194 argument is required, and this specification is not
195 included in the count of successful assignments returned by
196 .BR scanf ().
197 .TP
198 \(bu
199 An optional \(aqm\(aq character.
200 This is used with string conversions
201 .RI ( %s ,
202 .IR %c ,
203 .IR %[ ),
204 and relieves the caller of the
205 need to allocate a corresponding buffer to hold the input: instead,
206 .BR scanf ()
207 allocates a buffer of sufficient size,
208 and assigns the address of this buffer to the corresponding
209 .I pointer
210 argument, which should be a pointer to a
211 .I "char\ *"
212 variable (this variable does not need to be initialized before the call).
213 The caller should subsequently
214 .BR free (3)
215 this buffer when it is no longer required.
216 .TP
217 \(bu
218 An optional decimal integer which specifies the
219 .IR "maximum field width" .
220 Reading of characters stops either when this maximum is reached or
221 when a nonmatching character is found, whichever happens first.
222 Most conversions discard initial white space characters (the exceptions
223 are noted below),
224 and these discarded characters don't count toward the maximum field width.
225 String input conversions store a terminating null byte (\(aq\\0\(aq)
226 to mark the end of the input;
227 the maximum field width does not include this terminator.
228 .TP
229 \(bu
230 An optional
231 .IR "type modifier character" .
232 For example, the
233 .B l
234 type modifier is used with integer conversions such as
235 .B %d
236 to specify that the corresponding
237 .I pointer
238 argument refers to a
239 .I "long int"
240 rather than a pointer to an
241 .IR int .
242 .TP
243 \(bu
244 A
245 .I "conversion specifier"
246 that specifies the type of input conversion to be performed.
247 .PP
248 The conversion specifications in
249 .I format
250 are of two forms, either beginning with \(aq%\(aq or beginning with
251 "\fB%\fP\fIn\fP\fB$\fP".
252 The two forms should not be mixed in the same
253 .I format
254 string, except that a string containing
255 "\fB%\fP\fIn\fP\fB$\fP"
256 specifications can include
257 .B %%
258 and
259 .BR %* .
260 If
261 .I format
262 contains \(aq%\(aq
263 specifications, then these correspond in order with successive
264 .I pointer
265 arguments.
266 In the
267 "\fB%\fP\fIn\fP\fB$\fP"
268 form (which is specified in POSIX.1-2001, but not C99),
269 .I n
270 is a decimal integer that specifies that the converted input should
271 be placed in the location referred to by the
272 .IR n -th
273 .I pointer
274 argument following
275 .IR format .
276 .SS Conversions
277 The following
278 .I "type modifier characters"
279 can appear in a conversion specification:
280 .TP
281 .B h
282 Indicates that the conversion will be one of
283 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
284 and the next pointer is a pointer to a
285 .I short int
286 or
287 .I unsigned short int
288 (rather than
289 .IR int ).
290 .TP
291 .B hh
292 As for
293 .BR h ,
294 but the next pointer is a pointer to a
295 .I signed char
296 or
297 .IR "unsigned char" .
298 .TP
299 .B j
300 As for
301 .BR h ,
302 but the next pointer is a pointer to an
303 .I intmax_t
304 or a
305 .IR uintmax_t .
306 This modifier was introduced in C99.
307 .TP
308 .B l
309 Indicates either that the conversion will be one of
310 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
311 and the next pointer is a pointer to a
312 .I long int
313 or
314 .I unsigned long int
315 (rather than
316 .IR int ),
317 or that the conversion will be one of
318 \fBe\fP, \fBf\fP, or \fBg\fP
319 and the next pointer is a pointer to
320 .I double
321 (rather than
322 .IR float ).
323 Specifying two
324 .B l
325 characters is equivalent to
326 .BR L .
327 If used with
328 .B %c
329 or
330 .BR %s ,
331 the corresponding parameter is considered
332 as a pointer to a wide character or wide-character string respectively.
333 .\" This use of l was introduced in Amendment 1 to ISO C90.
334 .TP
335 .B L
336 Indicates that the conversion will be either
337 \fBe\fP, \fBf\fP, or \fBg\fP
338 and the next pointer is a pointer to
339 .I "long double"
340 or the conversion will be
341 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
342 and the next pointer is a pointer to
343 .IR "long long" .
344 .\" MTK, Jul 05: The following is no longer true for modern
345 .\" ANSI C (i.e., C99):
346 .\" (Note that long long is not an
347 .\" ANSI C
348 .\" type. Any program using this will not be portable to all
349 .\" architectures).
350 .TP
351 .B q
352 equivalent to
353 .BR L .
354 This specifier does not exist in ANSI C.
355 .TP
356 .B t
357 As for
358 .BR h ,
359 but the next pointer is a pointer to a
360 .IR ptrdiff_t .
361 This modifier was introduced in C99.
362 .TP
363 .B z
364 As for
365 .BR h ,
366 but the next pointer is a pointer to a
367 .IR size_t .
368 This modifier was introduced in C99.
369 .PP
370 The following
371 .I "conversion specifiers"
372 are available:
373 .TP
374 .B %
375 Matches a literal \(aq%\(aq.
376 That is,
377 .B %\&%
378 in the format string matches a
379 single input \(aq%\(aq character.
380 No conversion is done (but initial white space characters are discarded),
381 and assignment does not occur.
382 .TP
383 .B d
384 Matches an optionally signed decimal integer;
385 the next pointer must be a pointer to
386 .IR int .
387 .TP
388 .B D
389 Equivalent to
390 .IR ld ;
391 this exists only for backward compatibility.
392 (Note: thus only in libc4.
393 In libc5 and glibc the
394 .B %D
395 is silently ignored, causing old programs to fail mysteriously.)
396 .TP
397 .B i
398 Matches an optionally signed integer; the next pointer must be a pointer to
399 .IR int .
400 The integer is read in base 16 if it begins with
401 .I 0x
402 or
403 .IR 0X ,
404 in base 8 if it begins with
405 .IR 0 ,
406 and in base 10 otherwise.
407 Only characters that correspond to the base are used.
408 .TP
409 .B o
410 Matches an unsigned octal integer; the next pointer must be a pointer to
411 .IR "unsigned int" .
412 .TP
413 .B u
414 Matches an unsigned decimal integer; the next pointer must be a
415 pointer to
416 .IR "unsigned int" .
417 .TP
418 .B x
419 Matches an unsigned hexadecimal integer; the next pointer must
420 be a pointer to
421 .IR "unsigned int" .
422 .TP
423 .B X
424 Equivalent to
425 .BR x .
426 .TP
427 .B f
428 Matches an optionally signed floating-point number; the next pointer must
429 be a pointer to
430 .IR float .
431 .TP
432 .B e
433 Equivalent to
434 .BR f .
435 .TP
436 .B g
437 Equivalent to
438 .BR f .
439 .TP
440 .B E
441 Equivalent to
442 .BR f .
443 .TP
444 .B a
445 (C99) Equivalent to
446 .BR f .
447 .TP
448 .B s
449 Matches a sequence of non-white-space characters;
450 the next pointer must be a pointer to the initial element of a
451 character array that is long enough to hold the input sequence and
452 the terminating null byte (\(aq\\0\(aq), which is added automatically.
453 The input string stops at white space or at the maximum field
454 width, whichever occurs first.
455 .TP
456 .B c
457 Matches a sequence of characters whose length is specified by the
458 .I maximum field width
459 (default 1); the next pointer must be a pointer to
460 .IR char ,
461 and there must be enough room for all the characters
462 (no terminating null byte is added).
463 The usual skip of leading white space is suppressed.
464 To skip white space first, use an explicit space in the format.
465 .TP
466 .B \&[
467 Matches a nonempty sequence of characters from the specified set of
468 accepted characters; the next pointer must be a pointer to
469 .IR char ,
470 and there must be enough room for all the characters in the string, plus a
471 terminating null byte.
472 The usual skip of leading white space is suppressed.
473 The string is to be made up of characters in (or not in) a particular set;
474 the set is defined by the characters between the open bracket
475 .B [
476 character and a close bracket
477 .B ]
478 character.
479 The set
480 .I excludes
481 those characters if the first character after the open bracket is a
482 circumflex
483 .RB ( ^ ).
484 To include a close bracket in the set, make it the first character after
485 the open bracket or the circumflex; any other position will end the set.
486 The hyphen character
487 .B \-
488 is also special; when placed between two other characters, it adds all
489 intervening characters to the set.
490 To include a hyphen, make it the last
491 character before the final close bracket.
492 For instance,
493 .B [^]0\-9\-]
494 means
495 the set "everything except close bracket, zero through nine, and hyphen".
496 The string ends with the appearance of a character not in the (or, with a
497 circumflex, in) set or when the field width runs out.
498 .TP
499 .B p
500 Matches a pointer value (as printed by
501 .B %p
502 in
503 .BR printf (3);
504 the next pointer must be a pointer to a pointer to
505 .IR void .
506 .TP
507 .B n
508 Nothing is expected; instead, the number of characters consumed thus far
509 from the input is stored through the next pointer, which must be a pointer
510 to
511 .IR int .
512 This is
513 .I not
514 a conversion and does
515 .I not
516 increase the count returned by the function.
517 The assignment can be suppressed with the
518 .B *
519 assignment-suppression character, but the effect on the
520 return value is undefined.
521 Therefore
522 .B %*n
523 conversions should not be used.
524 .SH RETURN VALUE
525 On success, these functions return the number of input items
526 successfully matched and assigned;
527 this can be fewer than provided for,
528 or even zero, in the event of an early matching failure.
529
530 The value
531 .B EOF
532 is returned if the end of input is reached before either the first
533 successful conversion or a matching failure occurs.
534 .B EOF
535 is also returned if a read error occurs,
536 in which case the error indicator for the stream (see
537 .BR ferror (3))
538 is set, and
539 .I errno
540 is set to indicate the error.
541 .SH ERRORS
542 .TP
543 .B EAGAIN
544 The file descriptor underlying
545 .I stream
546 is marked nonblocking, and the read operation would block.
547 .TP
548 .B EBADF
549 The file descriptor underlying
550 .I stream
551 is invalid, or not open for reading.
552 .TP
553 .B EILSEQ
554 Input byte sequence does not form a valid character.
555 .TP
556 .B EINTR
557 The read operation was interrupted by a signal; see
558 .BR signal (7).
559 .TP
560 .B EINVAL
561 Not enough arguments; or
562 .I format
563 is NULL.
564 .TP
565 .B ENOMEM
566 Out of memory.
567 .TP
568 .B ERANGE
569 The result of an integer conversion would exceed the size
570 that can be stored in the corresponding integer type.
571 .SH ATTRIBUTES
572 For an explanation of the terms used in this section, see
573 .BR attributes (7).
574 .TS
575 allbox;
576 lbw20 lb lb
577 l l l.
578 Interface Attribute Value
579 T{
580 .BR scanf (),
581 .BR fscanf (),
582 .br
583 .BR sscanf (),
584 .BR vscanf (),
585 .br
586 .BR vsscanf (),
587 .BR vfscanf ()
588 T} Thread safety MT-Safe locale
589 .TE
590
591 .SH CONFORMING TO
592 The functions
593 .BR fscanf (),
594 .BR scanf (),
595 and
596 .BR sscanf ()
597 conform to C89 and C99 and POSIX.1-2001.
598 These standards do not specify the
599 .B ERANGE
600 error.
601 .PP
602 The
603 .B q
604 specifier is the 4.4BSD notation for
605 .IR "long long" ,
606 while
607 .B ll
608 or the usage of
609 .B L
610 in integer conversions is the GNU notation.
611 .PP
612 The Linux version of these functions is based on the
613 .I GNU
614 .I libio
615 library.
616 Take a look at the
617 .I info
618 documentation of
619 .I GNU
620 .I libc (glibc-1.08)
621 for a more concise description.
622 .SH NOTES
623 .SS The 'a' assignment-allocation modifier
624 Originally, the GNU C library supported dynamic allocation for string inputs
625 (as a nonstandard extension) via the
626 .B a
627 character.
628 (This feature is present at least as far back as glibc 2.0.)
629 Thus, one could write the following to have
630 .BR scanf ()
631 allocate a buffer for an input string,
632 with a pointer to that buffer being returned in
633 .IR *buf :
634
635 char *buf;
636 scanf("%as", &buf);
637 .PP
638 The use of the letter
639 .B a
640 for this purpose was problematic, since
641 .B a
642 is also specified by the ISO C standard as a synonym for
643 .B f
644 (floating-point input).
645 POSIX.1-2008 instead specifies the
646 .B m
647 modifier for assignment allocation (as documented in DESCRIPTION, above).
648 .PP
649 Note that the
650 .B a
651 modifier is not available if the program is compiled with
652 .I "gcc -std=c99"
653 or
654 .IR "gcc -D_ISOC99_SOURCE"
655 (unless
656 .B _GNU_SOURCE
657 is also specified), in which case the
658 .B a
659 is interpreted as a specifier for floating-point numbers (see above).
660
661 Support for the
662 .B m
663 modifier was added to glibc starting with version 2.7,
664 and new programs should use that modifier instead of
665 .BR a .
666
667 As well as being standardized by POSIX, the
668 .B m
669 modifier has the following further advantages over
670 the use of
671 .BR a:
672 .IP * 2
673 It may also be applied to
674 .B %c
675 conversion specifiers (e.g.,
676 .BR %3mc ).
677 .IP *
678 It avoids ambiguity with respect to the
679 .B %a
680 floating-point conversion specifier (and is unaffected by
681 .IR "gcc -std=c99"
682 etc.).
683 .SH BUGS
684 All functions are fully C89 conformant, but provide the
685 additional specifiers
686 .B q
687 and
688 .B a
689 as well as an additional behavior of the
690 .B L
691 and
692 .B l
693 specifiers.
694 The latter may be considered to be a bug, as it changes the
695 behavior of specifiers defined in C89.
696 .PP
697 Some combinations of the type modifiers and conversion
698 specifiers defined by ANSI C do not make sense
699 (e.g.,
700 .BR "%Ld" ).
701 While they may have a well-defined behavior on Linux, this need not
702 to be so on other architectures.
703 Therefore it usually is better to use
704 modifiers that are not defined by ANSI C at all, that is, use
705 .B q
706 instead of
707 .B L
708 in combination with
709 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
710 conversions or
711 .BR ll .
712 .PP
713 The usage of
714 .B q
715 is not the same as on 4.4BSD,
716 as it may be used in float conversions equivalently to
717 .BR L .
718 .SH EXAMPLE
719 To use the dynamic allocation conversion specifier, specify
720 .B m
721 as a length modifier (thus
722 .B %ms
723 or
724 \fB%m[\fP\fIrange\fP\fB]\fP).
725 The caller must
726 .BR free (3)
727 the returned string, as in the following example:
728 .in +4n
729 .nf
730
731 char *p;
732 int n;
733
734 errno = 0;
735 n = scanf("%m[a-z]", &p);
736 if (n == 1) {
737 printf("read: %s\\n", p);
738 free(p);
739 } else if (errno != 0) {
740 perror("scanf");
741 } else {
742 fprintf(stderr, "No matching characters\\n");
743 }
744 .fi
745 .in
746 .PP
747 As shown in the above example, it is necessary to call
748 .BR free (3)
749 only if the
750 .BR scanf ()
751 call successfully read a string.
752 .SH SEE ALSO
753 .BR getc (3),
754 .BR printf (3),
755 .BR setlocale (3),
756 .BR strtod (3),
757 .BR strtol (3),
758 .BR strtoul (3)