2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<sscanf>>, <<fscanf>>, <<scanf>>---scan and format input
38 int scanf(const char *restrict <[format]>, ...);
39 int fscanf(FILE *restrict <[fd]>, const char *restrict <[format]>, ...);
40 int sscanf(const char *restrict <[str]>, const char *restrict <[format]>, ...);
42 int _scanf_r(struct _reent *<[ptr]>, const char *restrict <[format]>, ...);
43 int _fscanf_r(struct _reent *<[ptr]>, FILE *restrict <[fd]>,
44 const char *restrict <[format]>, ...);
45 int _sscanf_r(struct _reent *<[ptr]>, const char *restrict <[str]>,
46 const char *restrict <[format]>, ...);
49 <<scanf>> scans a series of input fields from standard input,
50 one character at a time. Each field is interpreted according to
51 a format specifier passed to <<scanf>> in the format string at
52 <<*<[format]>>>. <<scanf>> stores the interpreted input from
53 each field at the address passed to it as the corresponding argument
54 following <[format]>. You must supply the same number of
55 format specifiers and address arguments as there are input fields.
57 There must be sufficient address arguments for the given format
58 specifiers; if not the results are unpredictable and likely
59 disasterous. Excess address arguments are merely ignored.
61 <<scanf>> often produces unexpected results if the input diverges from
62 an expected pattern. Since the combination of <<gets>> or <<fgets>>
63 followed by <<sscanf>> is safe and easy, that is the preferred way
64 to be certain that a program is synchronized with input at the end
67 <<fscanf>> and <<sscanf>> are identical to <<scanf>>, other than the
68 source of input: <<fscanf>> reads from a file, and <<sscanf>>
71 The routines <<_scanf_r>>, <<_fscanf_r>>, and <<_sscanf_r>> are reentrant
72 versions of <<scanf>>, <<fscanf>>, and <<sscanf>> that take an additional
73 first argument pointing to a reentrancy structure.
75 The string at <<*<[format]>>> is a character sequence composed
76 of zero or more directives. Directives are composed of
77 one or more whitespace characters, non-whitespace characters,
78 and format specifications.
80 Whitespace characters are blank (<< >>), tab (<<\t>>), or
82 When <<scanf>> encounters a whitespace character in the format string
83 it will read (but not store) all consecutive whitespace characters
84 up to the next non-whitespace character in the input.
86 Non-whitespace characters are all other ASCII characters except the
87 percent sign (<<%>>). When <<scanf>> encounters a non-whitespace
88 character in the format string it will read, but not store
89 a matching non-whitespace character.
91 Format specifications tell <<scanf>> to read and convert characters
92 from the input field into specific types of values, and store then
93 in the locations specified by the address arguments.
95 Trailing whitespace is left unread unless explicitly
96 matched in the format string.
98 The format specifiers must begin with a percent sign (<<%>>)
99 and have the following form:
101 . %[*][<[width]>][<[size]>]<[type]>
103 Each format specification begins with the percent character (<<%>>).
104 The other fields are:
108 an optional marker; if present, it suppresses interpretation and
109 assignment of this input field.
113 an optional maximum field width: a decimal integer,
114 which controls the maximum number of characters that
115 will be read before converting the current input field. If the
116 input field has fewer than <[width]> characters, <<scanf>>
117 reads all the characters in the field, and then
118 proceeds with the next field and its format specification.
120 If a whitespace or a non-convertable character occurs
121 before <[width]> character are read, the characters up
122 to that character are read, converted, and stored.
123 Then <<scanf>> proceeds to the next format specification.
127 <<h>>, <<j>>, <<l>>, <<L>>, <<t>>, and <<z>> are optional size
128 characters which override the default way that <<scanf>>
129 interprets the data type of the corresponding argument.
131 @multitable @columnfractions 0.18 0.30 0.52
142 convert input to char, store in char object
148 convert input to short, store in short object
152 D, I, O, U, X, e, f, c, s, p
160 convert input to intmax_t, store in intmax_t object
172 convert input to long, store in long object
178 convert input to double, store in a double object
182 D, I, O, U, X, c, s, p
190 convert to long long, store in long long object
196 convert to long long, store in long long object
202 convert to long double, store in long double object
214 convert input to ptrdiff_t, store in ptrdiff_t object
226 convert input to size_t, store in size_t object
237 A character to specify what kind of conversion
238 <<scanf>> performs. Here is a table of the conversion
243 No conversion is done; the percent character (<<%>>) is stored.
246 Scans one character. Corresponding <[arg]>: <<(char *arg)>>.
249 Reads a character string into the array supplied.
250 Corresponding <[arg]>: <<(char arg[])>>.
253 Reads a non-empty character string into memory
254 starting at <[arg]>. This area must be large
255 enough to accept the sequence and a
256 terminating null character which will be added
257 automatically. (<[pattern]> is discussed in the paragraph following
258 this table). Corresponding <[arg]>: <<(char *arg)>>.
261 Reads a decimal integer into the corresponding <[arg]>: <<(int *arg)>>.
264 Reads a decimal integer into the corresponding
265 <[arg]>: <<(long *arg)>>.
268 Reads an octal integer into the corresponding <[arg]>: <<(int *arg)>>.
271 Reads an octal integer into the corresponding <[arg]>: <<(long *arg)>>.
274 Reads an unsigned decimal integer into the corresponding
275 <[arg]>: <<(unsigned int *arg)>>.
278 Reads an unsigned decimal integer into the corresponding <[arg]>:
279 <<(unsigned long *arg)>>.
282 Read a hexadecimal integer into the corresponding <[arg]>:
286 Read a floating-point number into the corresponding <[arg]>:
290 Read a floating-point number into the corresponding <[arg]>:
294 Reads a decimal, octal or hexadecimal integer into the
295 corresponding <[arg]>: <<(int *arg)>>.
298 Reads a decimal, octal or hexadecimal integer into the
299 corresponding <[arg]>: <<(long *arg)>>.
302 Stores the number of characters read in the corresponding
303 <[arg]>: <<(int *arg)>>.
306 Stores a scanned pointer. ANSI C leaves the details
307 to each implementation; this implementation treats
308 <<%p>> exactly the same as <<%U>>. Corresponding
309 <[arg]>: <<(void **arg)>>.
312 A <[pattern]> of characters surrounded by square brackets can be used
313 instead of the <<s>> type character. <[pattern]> is a set of
314 characters which define a search set of possible characters making up
315 the <<scanf>> input field. If the first character in the brackets is a
316 caret (<<^>>), the search set is inverted to include all ASCII characters
317 except those between the brackets. There is also a range facility
318 which you can use as a shortcut. <<%[0-9] >> matches all decimal digits.
319 The hyphen must not be the first or last character in the set.
320 The character prior to the hyphen must be lexically less than the
323 Here are some <[pattern]> examples:
326 matches strings containing only <<a>>, <<b>>, <<c>>, and <<d>>.
329 matches strings containing any characters except <<a>>, <<b>>,
333 matches strings containing <<A>>, <<B>>, <<C>>, <<D>>, <<W>>,
337 matches the characters <<z>>, <<->>, and <<a>>
340 Floating point numbers (for field types <<e>>, <<f>>, <<g>>, <<E>>,
341 <<F>>, <<G>>) must correspond to the following general form:
343 . [+/-] ddddd[.]ddd [E|e[+|-]ddd]
345 where objects inclosed in square brackets are optional, and <<ddd>>
346 represents decimal, octal, or hexadecimal digits.
350 <<scanf>> returns the number of input fields successfully
351 scanned, converted and stored; the return value does
352 not include scanned fields which were not stored.
354 If <<scanf>> attempts to read at end-of-file, the return
357 If no fields were stored, the return value is <<0>>.
359 <<scanf>> might stop scanning a particular field before
360 reaching the normal field end character, or may
363 <<scanf>> stops scanning and storing the current field
364 and moves to the next input field (if any)
365 in any of the following situations:
368 o The assignment suppressing character (<<*>>) appears
369 after the <<%>> in the format specification; the current
370 input field is scanned but not stored.
372 o <[width]> characters have been read (<[width]> is a
373 width specification, a positive decimal integer).
375 o The next character read cannot be converted
376 under the the current format (for example,
377 if a <<Z>> is read when the format is decimal).
379 o The next character in the input field does not appear
380 in the search set (or does appear in the inverted search set).
383 When <<scanf>> stops scanning the current input field for one of
384 these reasons, the next character is considered unread and
385 used as the first character of the following input field, or the
386 first character in a subsequent read operation on the input.
388 <<scanf>> will terminate under the following circumstances:
391 o The next character in the input field conflicts
392 with a corresponding non-whitespace character in the
395 o The next character in the input field is <<EOF>>.
397 o The format string has been exhausted.
400 When the format string contains a character sequence that is
401 not part of a format specification, the same character
402 sequence must appear in the input; <<scanf>> will
403 scan but not store the matched characters. If a
404 conflict occurs, the first conflicting character remains in the input
405 as if it had never been read.
410 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
411 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
424 sscanf (const char *__restrict str
,
425 const char * fmt
, ...)
431 f
._flags
= __SRD
| __SSTR
;
433 f
._bf
._base
= f
._p
= (unsigned char *) str
;
434 f
._bf
._size
= f
._r
= strlen (str
);
435 f
._read
= __seofread
;
438 f
._file
= -1; /* No file. */
440 ret
= __ssvfscanf_r (_REENT
, &f
, fmt
, ap
);
445 #ifdef _NANO_FORMATTED_IO
447 siscanf (const char *, const char *, ...)
448 _ATTRIBUTE ((__alias__("sscanf")));
451 #endif /* !_REENT_ONLY */
454 _sscanf_r (struct _reent
*ptr
,
455 const char *__restrict str
,
456 const char *__restrict fmt
, ...)
462 f
._flags
= __SRD
| __SSTR
;
464 f
._bf
._base
= f
._p
= (unsigned char *) str
;
465 f
._bf
._size
= f
._r
= strlen (str
);
466 f
._read
= __seofread
;
469 f
._file
= -1; /* No file. */
471 ret
= __ssvfscanf_r (ptr
, &f
, fmt
, ap
);
476 #ifdef _NANO_FORMATTED_IO
478 _siscanf_r (struct _reent
*, const char *, const char *, ...)
479 _ATTRIBUTE ((__alias__("_sscanf_r")));