sscanf---scan and format input
#include <stdio.h> int scanf(const char *format [, arg, ...]); int fscanf(FILE *fd, const char *format [, arg, ...]); int sscanf(const char *str, const char *format [, arg, ...]);
scanf scans a series of input fields from standard input,
one character at a time. Each field is interpreted according to
a format specifier passed to
scanf in the format string at
scanf stores the interpreted input from
each field at the address passed to it as the corresponding argument
following format. You must supply the same number of
format specifiers and address arguments as there are input fields.
There must be sufficient address arguments for the given format specifiers; if not the results are unpredictable and likely disasterous. Excess address arguments are merely ignored.
scanf often produces unexpected results if the input diverges from
an expected pattern. Since the combination of
sscanf is safe and easy, that is the preferred way
to be certain that a program is synchronized with input at the end
of a line.
sscanf are identical to
scanf, other than the
source of input:
fscanf reads from a file, and
from a string.
The string at
*format is a character sequence composed
of zero or more directives. Directives are composed of
one or more whitespace characters, non-whitespace characters,
and format specifications.
Whitespace characters are blank (
scanf encounters a whitespace character in the format string
it will read (but not store) all consecutive whitespace characters
up to the next non-whitespace character in the input.
Non-whitespace characters are all other ASCII characters except the
percent sign (
scanf encounters a non-whitespace
character in the format string it will read, but not store
a matching non-whitespace character.
Format specifications tell
scanf to read and convert characters
from the input field into specific types of values, and store then
in the locations specified by the address arguments.
Trailing whitespace is left unread unless explicitly matched in the format string.
The format specifiers must begin with a percent sign (
and have the following form:
Each format specification begins with the percent character (
The other fields are:
scanfreads all the characters in the field, and then proceeds with the next field and its format specification. If a whitespace or a non-convertable character occurs before width character are read, the characters up to that character are read, converted, and stored. Then
scanfproceeds to the next format specification.
Lare optional size characters which override the default way that
scanfinterprets the data type of the corresponding argument.
Modifier Type(s) h d, i, o, u, x convert input to short, store in short object h D, I, O, U, X no effect e, f, c, s, n, p l d, i, o, u, x convert input to long, store in long object l e, f, g convert input to double store in a double object l D, I, O, U, X no effect c, s, n, p L d, i, o, u, x convert to long double, store in long double L all others no effect
scanfperforms. Here is a table of the conversion characters:
%) is stored.
(unsigned int *arg).
(unsigned long *arg).
e, f, g
E, F, G
%pexactly the same as
%U. Corresponding arg:
stype character. pattern is a set of characters which define a search set of possible characters making up the
scanfinput field. If the first character in the brackets is a caret (
^), the search set is inverted to include all ASCII characters except those between the brackets. There is also a range facility which you can use as a shortcut.
%[0-9]matches all decimal digits. The hyphen must not be the first or last character in the set. The character prior to the hyphen must be lexically less than the character after it. Here are some pattern examples:
G) must correspond to the following general form:
[+/-] ddddd[.]ddd [E|e[+|-]ddd]where objects inclosed in square brackets are optional, and
dddrepresents decimal, octal, or hexadecimal digits.
scanf returns the number of input fields successfully
scanned, converted and stored; the return value does
not include scanned fields which were not stored.
scanf attempts to read at end-of-file, the return
If no fields were stored, the return value is
scanf might stop scanning a particular field before
reaching the normal field end character, or may
scanf stops scanning and storing the current field
and moves to the next input field (if any)
in any of the following situations:
*) appears after the
%in the format specification; the current input field is scanned but not stored.
Zis read when the format is decimal).
scanf stops scanning the current input field for one of
these reasons, the next character is considered unread and
used as the first character of the following input field, or the
first character in a subsequent read operation on the input.
scanf will terminate under the following circumstances:
When the format string contains a character sequence that is
not part of a format specification, the same character
sequence must appear in the input;
scan but not store the matched characters. If a
conflict occurs, the first conflicting character remains in the input
as if it had never been read.
scanf is ANSI C.
Supporting OS subroutines required:
Go to the first, previous, next, last section, table of contents.