2 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
3 .\" All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Chris Torek and the American National Standards Committee X3,
7 .\" on Information Processing Systems.
9 .\" SPDX-License-Identifier: BSD-4-Clause-UC
11 .\" @(#)scanf.3 6.14 (Berkeley) 1/8/93
13 .\" Converted for Linux, Mon Nov 29 15:22:01 1993, faith@cs.unc.edu
14 .\" modified to resemble the GNU libio setup used in the Linux libc
15 .\" used in versions 4.x (x>4) and 5 Helmut.Geyer@iwr.uni-heidelberg.de
16 .\" Modified, aeb, 970121
17 .\" 2005-07-14, mtk, added description of %n$ form; various text
18 .\" incorporated from the GNU C library documentation ((C) The
19 .\" Free Software Foundation); other parts substantially rewritten.
22 .\" Add ERRORS section.
23 .\" Document the 'a' and 'm' modifiers for dynamic string allocation.
25 .TH sscanf 3 (date) "Linux man-pages (unreleased)"
27 sscanf, vsscanf \- input string format conversion
30 .RI ( libc ", " \-lc )
35 .BI "int sscanf(const char *restrict " str ,
36 .BI " const char *restrict " format ", ...);"
38 .B #include <stdarg.h>
40 .BI "int vsscanf(const char *restrict " str ,
41 .BI " const char *restrict " format ", va_list " ap );
45 Feature Test Macro Requirements for glibc (see
46 .BR feature_test_macros (7)):
51 _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
56 family of functions scans formatted input according to
59 This format may contain
60 .IR "conversion specifications" ;
61 the results from such conversions, if any,
62 are stored in the locations pointed to by the
68 argument must be of a type that is appropriate for the value returned
69 by the corresponding conversion specification.
71 If the number of conversion specifications in
75 arguments, the results are undefined.
78 arguments exceeds the number of conversion specifications, then the excess
80 arguments are evaluated, but are otherwise ignored.
84 read their input from the string pointed to by
89 function is analogous to
94 string consists of a sequence of
96 which describe how to process the sequence of input characters.
97 If processing of a directive fails, no further input is read, and
100 A "failure" can be either of the following:
101 .IR "input failure" ,
102 meaning that input characters were unavailable, or
103 .IR "matching failure" ,
104 meaning that the input was inappropriate (see below).
106 A directive is one of the following:
109 A sequence of white-space characters (space, tab, newline, etc.; see
111 This directive matches any amount of white space,
112 including none, in the input.
115 An ordinary character (i.e., one other than white space or \[aq]%\[aq]).
116 This character must exactly match the next character of input.
119 A conversion specification,
120 which commences with a \[aq]%\[aq] (percent) character.
121 A sequence of characters from the input is converted according to
122 this specification, and the result is placed in the corresponding
125 If the next item of input does not match the conversion specification,
126 the conversion fails\[em]this is a
127 .IR "matching failure" .
130 .I conversion specification
133 begins with either the character \[aq]%\[aq] or the character sequence
134 "\fB%\fP\fIn\fP\fB$\fP"
135 (see below for the distinction) followed by:
138 An optional \[aq]*\[aq] assignment-suppression character:
140 reads input as directed by the conversion specification,
141 but discards the input.
144 argument is required, and this specification is not
145 included in the count of successful assignments returned by
149 For decimal conversions, an optional quote character (\[aq]).
150 This specifies that the input number may include thousands'
151 separators as defined by the
153 category of the current locale.
156 The quote character may precede or follow the \[aq]*\[aq]
157 assignment-suppression character.
160 An optional \[aq]m\[aq] character.
161 This is used with string conversions
165 and relieves the caller of the
166 need to allocate a corresponding buffer to hold the input: instead,
168 allocates a buffer of sufficient size,
169 and assigns the address of this buffer to the corresponding
171 argument, which should be a pointer to a
173 variable (this variable does not need to be initialized before the call).
174 The caller should subsequently
176 this buffer when it is no longer required.
179 An optional decimal integer which specifies the
180 .IR "maximum field width" .
181 Reading of characters stops either when this maximum is reached or
182 when a nonmatching character is found, whichever happens first.
183 Most conversions discard initial white space characters (the exceptions
185 and these discarded characters don't count toward the maximum field width.
186 String input conversions store a terminating null byte (\[aq]\e0\[aq])
187 to mark the end of the input;
188 the maximum field width does not include this terminator.
192 .IR "type modifier character" .
195 type modifier is used with integer conversions such as
197 to specify that the corresponding
201 rather than a pointer to an
206 .I "conversion specifier"
207 that specifies the type of input conversion to be performed.
209 The conversion specifications in
211 are of two forms, either beginning with \[aq]%\[aq] or beginning with
212 "\fB%\fP\fIn\fP\fB$\fP".
213 The two forms should not be mixed in the same
215 string, except that a string containing
216 "\fB%\fP\fIn\fP\fB$\fP"
217 specifications can include
224 specifications, then these correspond in order with successive
228 "\fB%\fP\fIn\fP\fB$\fP"
229 form (which is specified in POSIX.1-2001, but not C99),
231 is a decimal integer that specifies that the converted input should
232 be placed in the location referred to by the
239 .I "type modifier characters"
240 can appear in a conversion specification:
243 Indicates that the conversion will be one of
244 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
245 and the next pointer is a pointer to a
255 but the next pointer is a pointer to a
258 .IR "unsigned char" .
263 but the next pointer is a pointer to an
267 This modifier was introduced in C99.
270 Indicates either that the conversion will be one of
271 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, \fBX\fP, or \fBn\fP
272 and the next pointer is a pointer to a
278 or that the conversion will be one of
279 \fBe\fP, \fBf\fP, or \fBg\fP
280 and the next pointer is a pointer to
288 the corresponding parameter is considered
289 as a pointer to a wide character or wide-character string respectively.
290 .\" This use of l was introduced in Amendment 1 to ISO C90.
294 Indicates that the conversion will be one of
304 and the next pointer is a pointer to a
307 .I unsigned long long
312 Indicates that the conversion will be either
313 \fBe\fP, \fBf\fP, or \fBg\fP
314 and the next pointer is a pointer to
318 the conversion will be
319 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, or \fBx\fP
320 and the next pointer is a pointer to
322 .\" MTK, Jul 05: The following is no longer true for modern
323 .\" ANSI C (i.e., C99):
324 .\" (Note that long long is not an
326 .\" type. Any program using this will not be portable to all
332 This specifier does not exist in ANSI C.
337 but the next pointer is a pointer to a
339 This modifier was introduced in C99.
344 but the next pointer is a pointer to a
346 This modifier was introduced in C99.
349 .I "conversion specifiers"
353 Matches a literal \[aq]%\[aq].
356 in the format string matches a
357 single input \[aq]%\[aq] character.
358 No conversion is done (but initial white space characters are discarded),
359 and assignment does not occur.
362 Matches an optionally signed decimal integer;
363 the next pointer must be a pointer to
369 .\" this exists only for backward compatibility.
370 .\" (Note: thus only in libc4
371 .\" In libc5 and glibc the
373 .\" is silently ignored, causing old programs to fail mysteriously.)
376 Matches an optionally signed integer; the next pointer must be a pointer to
378 The integer is read in base 16 if it begins with
382 in base 8 if it begins with
384 and in base 10 otherwise.
385 Only characters that correspond to the base are used.
388 Matches an unsigned octal integer; the next pointer must be a pointer to
392 Matches an unsigned decimal integer; the next pointer must be a
397 Matches an unsigned hexadecimal integer
398 (that may optionally begin with a prefix of
402 which is discarded); the next pointer must
411 Matches an optionally signed floating-point number; the next pointer must
432 Matches a sequence of non-white-space characters;
433 the next pointer must be a pointer to the initial element of a
434 character array that is long enough to hold the input sequence and
435 the terminating null byte (\[aq]\e0\[aq]), which is added automatically.
436 The input string stops at white space or at the maximum field
437 width, whichever occurs first.
440 Matches a sequence of characters whose length is specified by the
441 .I maximum field width
442 (default 1); the next pointer must be a pointer to
444 and there must be enough room for all the characters
445 (no terminating null byte is added).
446 The usual skip of leading white space is suppressed.
447 To skip white space first, use an explicit space in the format.
450 Matches a nonempty sequence of characters from the specified set of
451 accepted characters; the next pointer must be a pointer to
453 and there must be enough room for all the characters in the string, plus a
454 terminating null byte.
455 The usual skip of leading white space is suppressed.
456 The string is to be made up of characters in (or not in) a particular set;
457 the set is defined by the characters between the open bracket
459 character and a close bracket
464 those characters if the first character after the open bracket is a
467 To include a close bracket in the set, make it the first character after
468 the open bracket or the circumflex; any other position will end the set.
471 is also special; when placed between two other characters, it adds all
472 intervening characters to the set.
473 To include a hyphen, make it the last
474 character before the final close bracket.
478 the set "everything except close bracket, zero through nine, and hyphen".
479 The string ends with the appearance of a character not in the (or, with a
480 circumflex, in) set or when the field width runs out.
483 Matches a pointer value (as printed by
487 the next pointer must be a pointer to a pointer to
491 Nothing is expected; instead, the number of characters consumed thus far
492 from the input is stored through the next pointer, which must be a pointer
495 or variant whose size matches the (optionally)
496 supplied integer length modifier.
499 a conversion and does
501 increase the count returned by the function.
502 The assignment can be suppressed with the
504 assignment-suppression character, but the effect on the
505 return value is undefined.
508 conversions should not be used.
510 On success, these functions return the number of input items
511 successfully matched and assigned;
512 this can be fewer than provided for,
513 or even zero, in the event of an early matching failure.
517 is returned if the end of input is reached before either the first
518 successful conversion or a matching failure occurs.
522 Input byte sequence does not form a valid character.
525 Not enough arguments; or
532 For an explanation of the terms used in this section, see
538 Interface Attribute Value
544 T} Thread safety MT-Safe locale
553 specifier is the 4.4BSD notation for
559 in integer conversions is the GNU notation.
561 The Linux version of these functions is based on the
570 for a more concise description.
572 .SS The 'a' assignment-allocation modifier
573 Originally, the GNU C library supported dynamic allocation for string inputs
574 (as a nonstandard extension) via the
577 (This feature is present at least as far back as glibc 2.0.)
578 Thus, one could write the following to have
580 allocate a buffer for a string,
581 with a pointer to that buffer being returned in
587 sscanf(str, "%as", &buf);
591 The use of the letter
593 for this purpose was problematic, since
595 is also specified by the ISO C standard as a synonym for
597 (floating-point input).
598 POSIX.1-2008 instead specifies the
600 modifier for assignment allocation (as documented in DESCRIPTION, above).
604 modifier is not available if the program is compiled with
607 .I gcc\~\-D_ISOC99_SOURCE
610 is also specified), in which case the
612 is interpreted as a specifier for floating-point numbers (see above).
616 modifier was added to glibc 2.7,
617 and new programs should use that modifier instead of
620 As well as being standardized by POSIX, the
622 modifier has the following further advantages over
626 It may also be applied to
628 conversion specifiers (e.g.,
631 It avoids ambiguity with respect to the
633 floating-point conversion specifier (and is unaffected by
637 .SS Numeric conversion specifiers
638 Use of the numeric conversion specifiers produces Undefined Behavior
641 .UR https://port70.net/\:%7Ensz/\:c/\:c11/\:n1570.html\:#7.21.6.2p10
644 This is a bug in the ISO C standard,
645 and not an inherent design issue with the API.
647 current implementations are not safe from that bug,
648 so it is not recommended to use them.
650 programs should use functions such as
652 to parse numeric input.
654 mitigate it by specifying a maximum field width.
655 .SS Nonstandard modifiers
656 These functions are fully C99 conformant, but provide the
661 as well as an additional behavior of the
666 The latter may be considered to be a bug, as it changes the
667 behavior of modifiers defined in C99.
669 Some combinations of the type modifiers and conversion
670 specifiers defined by C99 do not make sense
673 While they may have a well-defined behavior on Linux, this need not
674 to be so on other architectures.
675 Therefore it usually is better to use
676 modifiers that are not defined by C99 at all, that is, use
681 \fBd\fP, \fBi\fP, \fBo\fP, \fBu\fP, \fBx\fP, and \fBX\fP
687 is not the same as on 4.4BSD,
688 as it may be used in float conversions equivalently to
691 To use the dynamic allocation conversion specifier, specify
693 as a length modifier (thus
696 \fB%m[\fP\fIrange\fP\fB]\fP).
699 the returned string, as in the following example:
707 n = sscanf(str, "%m[a\-z]", &p);
709 printf("read: %s\en", p);
711 } else if (errno != 0) {
714 fprintf(stderr, "No matching characters\en");
719 As shown in the above example, it is necessary to call
723 call successfully read a string.