mount_setattr.2: Reword the description of the 'propagation field'
[man-pages.git] / man3 / scanf.3
blobbd49a6caf4c450b234c6df52f6a92b0c746cf5e0
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  2021-03-22 "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 .PP
59 .BI "int scanf(const char *restrict " format ", ...);"
60 .BI "int fscanf(FILE *restrict " stream ,
61 .BI "           const char *restrict " format ", ...);"
62 .BI "int sscanf(const char *restrict " str ,
63 .BI "           const char *restrict " format ", ...);"
64 .PP
65 .B #include <stdarg.h>
66 .PP
67 .BI "int vscanf(const char *restrict " format ", va_list " ap );
68 .BI "int vfscanf(FILE *restrict " stream ,
69 .BI "           const char *restrict " format ", va_list " ap );
70 .BI "int vsscanf(const char *restrict " str ,
71 .BI "           const char *restrict " format ", va_list " ap );
72 .fi
73 .PP
74 .RS -4
75 Feature Test Macro Requirements for glibc (see
76 .BR feature_test_macros (7)):
77 .RE
78 .PP
79 .BR vscanf (),
80 .BR vsscanf (),
81 .BR vfscanf ():
82 .nf
83     _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
84 .fi
85 .SH DESCRIPTION
86 The
87 .BR scanf ()
88 family of functions scans input according to
89 .I format
90 as described below.
91 This format may contain
92 .IR "conversion specifications" ;
93 the results from such conversions, if any,
94 are stored in the locations pointed to by the
95 .I pointer
96 arguments that follow
97 .IR format .
98 Each
99 .I pointer
100 argument must be of a type that is appropriate for the value returned
101 by the corresponding conversion specification.
103 If the number of conversion specifications in
104 .I format
105 exceeds the number of
106 .I pointer
107 arguments, the results are undefined.
108 If the number of
109 .I pointer
110 arguments exceeds the number of conversion specifications, then the excess
111 .I pointer
112 arguments are evaluated, but are otherwise ignored.
115 .BR scanf ()
116 function reads input from the standard input stream
117 .IR stdin ,
118 .BR fscanf ()
119 reads input from the stream pointer
120 .IR stream ,
122 .BR sscanf ()
123 reads its input from the character string pointed to by
124 .IR str .
127 .BR vfscanf ()
128 function is analogous to
129 .BR vfprintf (3)
130 and reads input from the stream pointer
131 .I stream
132 using a variable argument list of pointers (see
133 .BR stdarg (3).
135 .BR vscanf ()
136 function scans a variable argument list from the standard input and the
137 .BR vsscanf ()
138 function scans it from a string; these are analogous to the
139 .BR vprintf (3)
141 .BR vsprintf (3)
142 functions respectively.
145 .I format
146 string consists of a sequence of
147 .I directives
148 which describe how to process the sequence of input characters.
149 If processing of a directive fails, no further input is read, and
150 .BR scanf ()
151 returns.
152 A "failure" can be either of the following:
153 .IR "input failure" ,
154 meaning that input characters were unavailable, or
155 .IR "matching failure" ,
156 meaning that the input was inappropriate (see below).
158 A directive is one of the following:
160 \(bu
161 A sequence of white-space characters (space, tab, newline, etc.; see
162 .BR isspace (3)).
163 This directive matches any amount of white space,
164 including none, in the input.
166 \(bu
167 An ordinary character (i.e., one other than white space or \(aq%\(aq).
168 This character must exactly match the next character of input.
170 \(bu
171 A conversion specification,
172 which commences with a \(aq%\(aq (percent) character.
173 A sequence of characters from the input is converted according to
174 this specification, and the result is placed in the corresponding
175 .I pointer
176 argument.
177 If the next item of input does not match the conversion specification,
178 the conversion fails\(emthis is a
179 .IR "matching failure" .
181 Each
182 .I conversion specification
184 .I format
185 begins with either the character \(aq%\(aq or the character sequence
186 "\fB%\fP\fIn\fP\fB$\fP"
187 (see below for the distinction) followed by:
189 \(bu
190 An optional \(aq*\(aq assignment-suppression character:
191 .BR scanf ()
192 reads input as directed by the conversion specification,
193 but discards the input.
194 No corresponding
195 .I pointer
196 argument is required, and this specification is not
197 included in the count of successful assignments returned by
198 .BR scanf ().
200 \(bu
201 For decimal conversions, an optional quote character (\(aq).
202 This specifies that the input number may include thousands'
203 separators as defined by the
204 .BR LC_NUMERIC
205 category of the current locale.
206 (See
207 .BR setlocale (3).)
208 The quote character may precede or follow the \(aq*\(aq
209 assignment-suppression character.
211 \(bu
212 An optional \(aqm\(aq character.
213 This is used with string conversions
214 .RI ( %s ,
215 .IR %c ,
216 .IR %[ ),
217 and relieves the caller of the
218 need to allocate a corresponding buffer to hold the input: instead,
219 .BR scanf ()
220 allocates a buffer of sufficient size,
221 and assigns the address of this buffer to the corresponding
222 .I pointer
223 argument, which should be a pointer to a
224 .I "char\ *"
225 variable (this variable does not need to be initialized before the call).
226 The caller should subsequently
227 .BR free (3)
228 this buffer when it is no longer required.
230 \(bu
231 An optional decimal integer which specifies the
232 .IR "maximum field width" .
233 Reading of characters stops either when this maximum is reached or
234 when a nonmatching character is found, whichever happens first.
235 Most conversions discard initial white space characters (the exceptions
236 are noted below),
237 and these discarded characters don't count toward the maximum field width.
238 String input conversions store a terminating null byte (\(aq\e0\(aq)
239 to mark the end of the input;
240 the maximum field width does not include this terminator.
242 \(bu
243 An optional
244 .IR "type modifier character" .
245 For example, the
246 .B l
247 type modifier is used with integer conversions such as
248 .B %d
249 to specify that the corresponding
250 .I pointer
251 argument refers to a
252 .I "long"
253 rather than a pointer to an
254 .IR int .
256 \(bu
258 .I "conversion specifier"
259 that specifies the type of input conversion to be performed.
261 The conversion specifications in
262 .I format
263 are of two forms, either beginning with \(aq%\(aq or beginning with
264 "\fB%\fP\fIn\fP\fB$\fP".
265 The two forms should not be mixed in the same
266 .I format
267 string, except that a string containing
268 "\fB%\fP\fIn\fP\fB$\fP"
269 specifications can include
270 .B %%
272 .BR %* .
274 .I format
275 contains \(aq%\(aq
276 specifications, then these correspond in order with successive
277 .I pointer
278 arguments.
279 In the
280 "\fB%\fP\fIn\fP\fB$\fP"
281 form (which is specified in POSIX.1-2001, but not C99),
282 .I n
283 is a decimal integer that specifies that the converted input should
284 be placed in the location referred to by the
285 .IR n -th
286 .I pointer
287 argument following
288 .IR format .
289 .SS Conversions
290 The following
291 .I "type modifier characters"
292 can appear in a conversion specification:
294 .B h
295 Indicates that the conversion will be one of
296 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
297 and the next pointer is a pointer to a
298 .I short
300 .I unsigned short
301 (rather than
302 .IR int ).
304 .B hh
305 As for
306 .BR h ,
307 but the next pointer is a pointer to a
308 .I signed char
310 .IR "unsigned char" .
312 .B j
313 As for
314 .BR h ,
315 but the next pointer is a pointer to an
316 .I intmax_t
317 or a
318 .IR uintmax_t .
319 This modifier was introduced in C99.
321 .B l
322 Indicates either that the conversion will be one of
323 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
324 and the next pointer is a pointer to a
325 .I long
327 .I unsigned long
328 (rather than
329 .IR int ),
330 or that the conversion will be one of
331 \fBe\fP, \fBf\fP, or \fBg\fP
332 and the next pointer is a pointer to
333 .I double
334 (rather than
335 .IR float ).
336 Specifying two
337 .B l
338 characters is equivalent to
339 .BR L .
340 If used with
341 .B %c
343 .BR %s ,
344 the corresponding parameter is considered
345 as a pointer to a wide character or wide-character string respectively.
346 .\" This use of l was introduced in Amendment 1 to ISO C90.
348 .B L
349 Indicates that the conversion will be either
350 \fBe\fP, \fBf\fP, or \fBg\fP
351 and the next pointer is a pointer to
352 .I "long double"
353 or the conversion will be
354 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
355 and the next pointer is a pointer to
356 .IR "long long" .
357 .\" MTK, Jul 05: The following is no longer true for modern
358 .\" ANSI C (i.e., C99):
359 .\" (Note that long long is not an
360 .\" ANSI C
361 .\" type. Any program using this will not be portable to all
362 .\" architectures).
364 .B q
365 equivalent to
366 .BR L .
367 This specifier does not exist in ANSI C.
369 .B t
370 As for
371 .BR h ,
372 but the next pointer is a pointer to a
373 .IR ptrdiff_t .
374 This modifier was introduced in C99.
376 .B z
377 As for
378 .BR h ,
379 but the next pointer is a pointer to a
380 .IR size_t .
381 This modifier was introduced in C99.
383 The following
384 .I "conversion specifiers"
385 are available:
387 .B %
388 Matches a literal \(aq%\(aq.
389 That is,
390 .B %\&%
391 in the format string matches a
392 single input \(aq%\(aq character.
393 No conversion is done (but initial white space characters are discarded),
394 and assignment does not occur.
396 .B d
397 Matches an optionally signed decimal integer;
398 the next pointer must be a pointer to
399 .IR int .
400 .\" .TP
401 .\" .B D
402 .\" Equivalent to
403 .\" .IR ld ;
404 .\" this exists only for backward compatibility.
405 .\" (Note: thus only in libc4
406 .\" In libc5 and glibc the
407 .\" .B %D
408 .\" is silently ignored, causing old programs to fail mysteriously.)
410 .B i
411 Matches an optionally signed integer; the next pointer must be a pointer to
412 .IR int .
413 The integer is read in base 16 if it begins with
414 .I 0x
416 .IR 0X ,
417 in base 8 if it begins with
418 .IR 0 ,
419 and in base 10 otherwise.
420 Only characters that correspond to the base are used.
422 .B o
423 Matches an unsigned octal integer; the next pointer must be a pointer to
424 .IR "unsigned int" .
426 .B u
427 Matches an unsigned decimal integer; the next pointer must be a
428 pointer to
429 .IR "unsigned int" .
431 .B x
432 Matches an unsigned hexadecimal integer
433 (that may optionally begin with a prefix of
434 .I 0x
436 .IR 0X ,
437 which is discarded); the next pointer must
438 be a pointer to
439 .IR "unsigned int" .
441 .B X
442 Equivalent to
443 .BR x .
445 .B f
446 Matches an optionally signed floating-point number; the next pointer must
447 be a pointer to
448 .IR float .
450 .B e
451 Equivalent to
452 .BR f .
454 .B g
455 Equivalent to
456 .BR f .
458 .B E
459 Equivalent to
460 .BR f .
462 .B a
463 (C99) Equivalent to
464 .BR f .
466 .B s
467 Matches a sequence of non-white-space characters;
468 the next pointer must be a pointer to the initial element of a
469 character array that is long enough to hold the input sequence and
470 the terminating null byte (\(aq\e0\(aq), which is added automatically.
471 The input string stops at white space or at the maximum field
472 width, whichever occurs first.
474 .B c
475 Matches a sequence of characters whose length is specified by the
476 .I maximum field width
477 (default 1); the next pointer must be a pointer to
478 .IR char ,
479 and there must be enough room for all the characters
480 (no terminating null byte is added).
481 The usual skip of leading white space is suppressed.
482 To skip white space first, use an explicit space in the format.
484 .B \&[
485 Matches a nonempty sequence of characters from the specified set of
486 accepted characters; the next pointer must be a pointer to
487 .IR char ,
488 and there must be enough room for all the characters in the string, plus a
489 terminating null byte.
490 The usual skip of leading white space is suppressed.
491 The string is to be made up of characters in (or not in) a particular set;
492 the set is defined by the characters between the open bracket
493 .B [
494 character and a close bracket
495 .B ]
496 character.
497 The set
498 .I excludes
499 those characters if the first character after the open bracket is a
500 circumflex
501 .RB ( \(ha ).
502 To include a close bracket in the set, make it the first character after
503 the open bracket or the circumflex; any other position will end the set.
504 The hyphen character
505 .B \-
506 is also special; when placed between two other characters, it adds all
507 intervening characters to the set.
508 To include a hyphen, make it the last
509 character before the final close bracket.
510 For instance,
511 .B [\(ha]0\-9\-]
512 means
513 the set "everything except close bracket, zero through nine, and hyphen".
514 The string ends with the appearance of a character not in the (or, with a
515 circumflex, in) set or when the field width runs out.
517 .B p
518 Matches a pointer value (as printed by
519 .B %p
521 .BR printf (3));
522 the next pointer must be a pointer to a pointer to
523 .IR void .
525 .B n
526 Nothing is expected; instead, the number of characters consumed thus far
527 from the input is stored through the next pointer, which must be a pointer
529 .IR int ,
530 or variant whose size matches the (optionally)
531 supplied integer length modifier.
532 This is
533 .I not
534 a conversion and does
535 .I not
536 increase the count returned by the function.
537 The assignment can be suppressed with the
538 .B *
539 assignment-suppression character, but the effect on the
540 return value is undefined.
541 Therefore
542 .B %*n
543 conversions should not be used.
544 .SH RETURN VALUE
545 On success, these functions return the number of input items
546 successfully matched and assigned;
547 this can be fewer than provided for,
548 or even zero, in the event of an early matching failure.
550 The value
551 .B EOF
552 is returned if the end of input is reached before either the first
553 successful conversion or a matching failure occurs.
554 .B EOF
555 is also returned if a read error occurs,
556 in which case the error indicator for the stream (see
557 .BR ferror (3))
558 is set, and
559 .I errno
560 is set to indicate the error.
561 .SH ERRORS
563 .B EAGAIN
564 The file descriptor underlying
565 .I stream
566 is marked nonblocking, and the read operation would block.
568 .B EBADF
569 The file descriptor underlying
570 .I stream
571 is invalid, or not open for reading.
573 .B EILSEQ
574 Input byte sequence does not form a valid character.
576 .B EINTR
577 The read operation was interrupted by a signal; see
578 .BR signal (7).
580 .B EINVAL
581 Not enough arguments; or
582 .I format
583 is NULL.
585 .B ENOMEM
586 Out of memory.
588 .B ERANGE
589 The result of an integer conversion would exceed the size
590 that can be stored in the corresponding integer type.
591 .SH ATTRIBUTES
592 For an explanation of the terms used in this section, see
593 .BR attributes (7).
594 .ad l
597 allbox;
598 lbx lb lb
599 l l l.
600 Interface       Attribute       Value
602 .BR scanf (),
603 .BR fscanf (),
604 .BR sscanf (),
605 .BR vscanf (),
606 .BR vsscanf (),
607 .BR vfscanf ()
608 T}      Thread safety   MT-Safe locale
612 .sp 1
613 .SH CONFORMING TO
614 The functions
615 .BR fscanf (),
616 .BR scanf (),
618 .BR sscanf ()
619 conform to C89 and C99 and POSIX.1-2001.
620 These standards do not specify the
621 .B ERANGE
622 error.
625 .B q
626 specifier is the 4.4BSD notation for
627 .IR "long long" ,
628 while
629 .B ll
630 or the usage of
631 .B L
632 in integer conversions is the GNU notation.
634 The Linux version of these functions is based on the
635 .I GNU
636 .I libio
637 library.
638 Take a look at the
639 .I info
640 documentation of
641 .I GNU
642 .I libc (glibc-1.08)
643 for a more concise description.
644 .SH NOTES
645 .SS The 'a' assignment-allocation modifier
646 Originally, the GNU C library supported dynamic allocation for string inputs
647 (as a nonstandard extension) via the
648 .B a
649 character.
650 (This feature is present at least as far back as glibc 2.0.)
651 Thus, one could write the following to have
652 .BR scanf ()
653 allocate a buffer for an input string,
654 with a pointer to that buffer being returned in
655 .IR *buf :
657     char *buf;
658     scanf("%as", &buf);
660 The use of the letter
661 .B a
662 for this purpose was problematic, since
663 .B a
664 is also specified by the ISO C standard as a synonym for
665 .B f
666 (floating-point input).
667 POSIX.1-2008 instead specifies the
668 .B m
669 modifier for assignment allocation (as documented in DESCRIPTION, above).
671 Note that the
672 .B a
673 modifier is not available if the program is compiled with
674 .I "gcc \-std=c99"
676 .IR "gcc \-D_ISOC99_SOURCE"
677 (unless
678 .B _GNU_SOURCE
679 is also specified), in which case the
680 .B a
681 is interpreted as a specifier for floating-point numbers (see above).
683 Support for the
684 .B m
685 modifier was added to glibc starting with version 2.7,
686 and new programs should use that modifier instead of
687 .BR a .
689 As well as being standardized by POSIX, the
690 .B m
691 modifier has the following further advantages over
692 the use of
693 .BR a:
694 .IP * 2
695 It may also be applied to
696 .B %c
697 conversion specifiers (e.g.,
698 .BR %3mc ).
699 .IP *
700 It avoids ambiguity with respect to the
701 .B %a
702 floating-point conversion specifier (and is unaffected by
703 .IR "gcc \-std=c99"
704 etc.).
705 .SH BUGS
706 All functions are fully C89 conformant, but provide the
707 additional specifiers
708 .B q
710 .B a
711 as well as an additional behavior of the
712 .B L
714 .B l
715 specifiers.
716 The latter may be considered to be a bug, as it changes the
717 behavior of specifiers defined in C89.
719 Some combinations of the type modifiers and conversion
720 specifiers defined by ANSI C do not make sense
721 (e.g.,
722 .BR "%Ld" ).
723 While they may have a well-defined behavior on Linux, this need not
724 to be so on other architectures.
725 Therefore it usually is better to use
726 modifiers that are not defined by ANSI C at all, that is, use
727 .B q
728 instead of
729 .B L
730 in combination with
731 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
732 conversions or
733 .BR ll .
735 The usage of
736 .B q
737 is not the same as on 4.4BSD,
738 as it may be used in float conversions equivalently to
739 .BR L .
740 .SH EXAMPLES
741 To use the dynamic allocation conversion specifier, specify
742 .B m
743 as a length modifier (thus
744 .B %ms
746 \fB%m[\fP\fIrange\fP\fB]\fP).
747 The caller must
748 .BR free (3)
749 the returned string, as in the following example:
751 .in +4n
753 char *p;
754 int n;
756 errno = 0;
757 n = scanf("%m[a\-z]", &p);
758 if (n == 1) {
759     printf("read: %s\en", p);
760     free(p);
761 } else if (errno != 0) {
762     perror("scanf");
763 } else {
764     fprintf(stderr, "No matching characters\en");
769 As shown in the above example, it is necessary to call
770 .BR free (3)
771 only if the
772 .BR scanf ()
773 call successfully read a string.
774 .SH SEE ALSO
775 .BR getc (3),
776 .BR printf (3),
777 .BR setlocale (3),
778 .BR strtod (3),
779 .BR strtol (3),
780 .BR strtoul (3)