Description |
The scanf function reads data from the input stream using
the getchar routine. Data input are stored in the locations
specified by argument according to the format
string fmtstr. Each argument
must be a pointer to a variable that corresponds to the type defined
in fmtstr. The type controls the interpretation of
the input data. The fmtstr may be composed of one
or more whitespace characters, non-whitespace characters, and format
specifications.
-
Whitespace characters, blank (' '), tab ('\t'), or newline
('\n'), cause scanf to skip whitespace characters in the
input stream. A single whitespace character in the format string
matches 0 or more whitespace characters in the input stream.
-
Non-whitespace characters, with the exception of the percent
sign ('%'), cause scanf to read but not store a matching
character from the input stream. The scanf function
terminates if the next character in the input stream does not match
the specified non-whitespace character.
-
Format specifications begin with a percent sign ('%') and cause
scanf to read and convert characters from the input stream
to the specified type values. The converted value is stored to an
argument from the parameter list. Characters
following a percent sign that are not recognized as a format
specification are treated as ordinary characters. For example, %%
matches a single percent sign in the input stream.
The format string is read from left to right. Characters that are
not part of the format specifications must match characters in the
input stream. These characters are read from the input stream but are
discarded and not stored. If a character in the input stream
conflicts with the format string, scanf terminates. Any
conflicting characters remain in the input stream.
The first format specification encountered in the format string
references the first argument after fmtstr. The scanf function converts input characters
and stores the value using the format specification. The second
format specification accesses the second argument
after fmtstr, and so on. If there are more
arguments than format specifications, the extra
arguments are ignored. Results are unpredictable
if there are not enough arguments for the format
specifications.
Values in the input stream are called input fields and are
delimited by whitespace characters. When converting input fields,
scanf ends a conversion for an argument when a whitespace
character or another unrecognized character is encountered.
Format specifications have the following format:
% <[>*<]> <[>width<]> <[>{b|h|l}<]> type
Each field in the format specification can be a single character
or a number which specifies a particular format option.
The type field is where a single character specifies
whether input characters are interpreted as a character, string, or
number. This field can be any one of the characters in the following
table.
Character |
Argument Type |
Input Format |
d |
int * |
Signed decimal number. |
i |
int * |
Signed decimal, hexadecimal, or octal
integer. |
u |
unsigned int * |
Unsigned decimal number. |
o |
unsigned int * |
Unsigned octal number. |
x |
unsigned int * |
Unsigned hexadecimal number. |
e |
float * |
Floating-point number. |
f |
float * |
Floating-point number. |
g |
float * |
Floating-point number. |
c |
char * |
A single character. |
s |
char * |
A string of characters terminated by
whitespace. |
An asterisk ('*') as the first character of a format specification
causes the input field to be scanned but not stored. The asterisk
suppresses assignment of the format specification.
The width field is a non-negative number that specifies the
maximum number of characters read from the input stream. No more than
width characters are read and converted for the corresponding
argument. However, fewer than width characters may be read if
a whitespace or other unrecognized character is encountered
first.
The optional characters b, h, and l may immediately precede the
type character to respectively specify char, short, or long versions
of the integer types d, i, u, o, and x. If l is applied to the
floating-point types f, e, and g, a pointer to a double is required
as an argument.
Note
-
This function is implementation-specific and is based on the
operation of the _getkey and putchar functions. These
functions, as provided in the standard library, read and write
characters using the microcontroller's serial port. Custom
functions may use other I/O devices.
|
Example |
#include <stdio.h>
void tst_scanf (void) {
char a;
int b;
long c;
unsigned char x;
unsigned int y;
unsigned long z;
float f,g;
char d, buf [10];
int argsread;
printf ("Enter a signed byte, int, and long\n");
argsread = scanf ("%bd %d %ld", &a, &b, &c);
printf ("%d arguments read\n", argsread);
printf ("Enter an unsigned byte, int, and long\n");
argsread = scanf ("%bu %u %lu", &x, &y, &z);
printf ("%d arguments read\n", argsread);
printf ("Enter a character and a string\n");
argsread = scanf ("%c %9s", &d, buf);
printf ("%d arguments read\n", argsread);
printf ("Enter two floating-point numbers\n");
argsread = scanf ("%f %f", &f, &g);
printf ("%d arguments read\n", argsread);
}
|