scanf,
fscanf,
sscanf
[scan and format input]
SYNOPSIS #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, ...]);DESCRIPTION
Each field is interpreted according to a format specifier passed to scanf in the format string at *format. 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 disastrous. Excess address arguments are merely ignored.
scanf often produces unexpected results if the input diverges from an expected pattern.
Since the combination of gets or fgets followed by 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. fscanf and sscanf are identical to scanf, other than the source of input: fscanf reads from a file, and sscanf 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 ( ), tab (\t), or newline (\n). When 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 (%). When 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.
%[*][width][size]type
Each format specification
begins with the percent character (%).
The other fields are described in the following discussions.
If the input field has fewer than width characters, scanf reads all the characters in the field, and then proceeds with the next field and its format specification.
If a whitespace or a non-convertible character occurs before width character are read, the characters up to that character are read, converted, and stored.
Then scanf proceeds to the next format specification.
Modifier | Type(s) | Effect |
h | d,i,o,u,x | Convert input to short, store in short object. |
h | D,I,O,U,X,
e, f,c,s,n,p |
No effect. |
l | d,i,o,u,x | Convert input to long, store in longobject. |
l | e,f,g | Convert input to double store in a doubleobject. |
l | D,I,O,U,X,
c,s,n,p |
No effect. |
L | d,i,o,u,x | Convert to long double, store in longdouble. |
L | All others | No effect. |
c
Scans
one character. Corresponding argument: char*arg.
s
Reads
a character string into the array supplied. Corresponding argument: char
arg[].
[pattern]
Reads
a non-empty character string into memory starting at arg.
This area must be large enough to accept the sequence and a terminating
null character which will be added automatically. Corresponding argument:
char
*arg.
The following are some pattern examples:
%[abcd] | Matches strings containing only a, b, c, and d. |
%[ˆabcd] | Matches strings containing any characters except a, b, c, or d. |
%[A-DW-Z] | Matches strings containing A, B, C, D, W, X, Y, Z. |
%[z-a] | Matches the characters z, -, and a. |
Floating point numbers (for field types e, f, g, E, F, G) must correspond to the following general form. Objects enclosed in square brackets are optional, and ddd represents decimal, octal, or hexadecimal digits.
[+/-] ddddd[.]ddd [E|e[+|-]ddd]
d
Reads a decimal integer into the corresponding argument:
int *arg.D
Reads a decimal integer into the corresponding argument:
long *arg.o
Reads an octal integer into the corresponding argument:
int *arg.O
Reads an octal integer into the corresponding argument:
long *arg.u
Reads an unsigned decimal integer into the corresponding argument: unsignedint *arg.U
Reads an unsigned decimal integer into the corresponding argument: unsignedlong *arg.x, X
Read a hexadecimal integer into the corresponding argument:
int *arg.e, f, g
Read a floating point number into the corresponding argument:
float *arg.E, F, G
Read a floating point number into the corresponding argument:
double *arg.i
Reads a decimal, octal or hexadecimal integer into the corresponding argument: int *arg.I
Reads a decimal, octal or hexadecimal integer into the corresponding argument: long *arg.n
Stores the number of characters read in the corresponding argument: int *arg.p
Stores a scanned pointer. ANSI C leaves the details to each implementation; this implementation treats %p exactly the same as %U. Corresponding argument: void **arg.
RETURNS
scanfreturns
the number of input fields successfully scanned, converted and stored;
the return value does not include scanned fields which were not stored.
If scanfattempts
to read at end-of-file, the return value is EOF.
If no fields were stored, the return value is 0.
scanfmight stop scanning a particular field before reaching the normal field end character, or may terminate entirely.
scanf stops scanning and storing the current field and moves to the next input field (if any) in any of the following situations.
scanf will terminate under the following circumstances.
COMPLIANCE
scanfis
ANSI C.
Supporting OS subroutines
required: close,
fstat,
isatty,
lseek,
read,
sbrk,
write.