scanf
Scans input from a stream
#include <stdio.h>
int scanf (
const char *format,
... );
The scanf function returns EOF when the scanning is terminated by reaching the end of the input stream. Otherwise, the number of input arguments for which values were successfully scanned and stored is returned.
The scanf function scans input from the file designated by stdin under control of the argument format. Following the format string is the list of addresses of items to receive values.
The format control string consists of zero or more format directives that specify acceptable input file data. Subsequent arguments are pointers to various types of objects that are assigned values as the format string is processed.
A format directive can be a sequence of one or more white-space characters, an ordinary character, or a conversion specifier. An ordinary character in the format string is any character, other than a white-space character or the percent (%) character, that is not part of a conversion specifier. A conversion specifier is a sequence of characters in the format string, which begins with a % and is followed, in sequence, by the following:
As each format directive in the format string is processed, the directive can successfully complete, fail because of a lack of input data, or fail because of a matching error as defined by the particular directive. If end-of-file is encountered on the input data before any characters that match the current directive have been processed (other than leading white-space where permitted), the directive fails for lack of data. If end-of-file occurs after a matching character has been processed, the directive is completed (unless a matching error occurs), and the function returns without processing the next directive. If a directive fails because of an input character mismatch, the character is left unread in the input stream. Trailing white-space characters, including newline characters, are not read unless matched by a directive. When a format directive fails, or the end of the format string is encountered, the scanning is completed and the function returns.
When one or more white-space characters—space, horizontal tab (\t), vertical tab (\v), form feed (\f), carriage return (\r), newline or line feed (\n)—occur in the format string, input data up to the first nonwhite-space character is read, or until no more data remains. If no white-space characters are found in the input data, the scanning is complete and the function returns.
An ordinary character in the format string is expected to match the same character in the input stream.
A conversion specifier in the format string is processed as follows:
A pointer type specification is used to indicate the type of pointer used to locate the next argument to be scanned:
The pointer type defaults to that used for data in the memory model for which the program has been compiled.
A type length specifier affects the conversion as follows:
The valid conversion type specifiers are:
A conversion type specifier of % is treated as a single ordinary character that matches a single % character in the input data. A conversion type specifier other than those listed above causes scanning to terminate the function to return.
The line
scanf ("%s%*f%3hx%d", name, &hexnum, &decnum)
with input
some_string 34.555e-3 abc1234
copies some_string into the array name, skip 34.555e-3, assign 0xabc to hexnum and 1234 to decnum. The return value is 3.
The line
char fmt[100];
strcpy (fmt, "%[abcdefghijklmnopqrstuvwxyz");
strcat (fmt,"[ABCDEFGHIJKLMNOPQRSTUVWZ]%*2s%[W\n]");
scanf (fmt, string1, string2)
with input
They may look alike, but they don’t perform alike.
assigns
"They may look alike"
to string1, skip the comma and the space, and assign
" but they don’t perform alike.".
to string2. (The %*2s only matches the ","; the next blank terminates that field.)
To scan a date in the form "Saturday April 18 1991":
#include <stdio.h>
int day, year;
char weekday[10], month[12];
scanf ("%s %s %d %d", weekday, month, &day, &year);