Считать ввод из стандартного потока ввода (stdin)
#include <stdio.h>int scanf( const char *format,... );
libc
The scanf() function scans input from stdin under control of the format argument, assigning values to the remaining arguments.
Format control string
The format control string consists of zero or more format directives that specify what you consider to be acceptable input data. Subsequent arguments are pointers to various types of objects that the function assigns values to as it processes the format string.
A format directive can be a sequence of one or more whitespace characters or:
h
, L
, or l
c
, d
, e
, f
, g
, i
, n
, o
, p
, s
, u
, X
, x
, [
As each format directive in the format string is processed, the directive may successfully complete, fail because of a lack of input data, or fail because of a matching error as defined by the 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 whitespace, 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 whitespace characters, including newline characters, aren't 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 whitespace characters (space, horizontal tab \t, vertical tab \v, form feed \f, carriage return \r, newline or linefeed \n) occur in the format string, input data up to the first non-whitespace character is read, or until no more data remains. If no whitespace 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.
Conversion specifiers
A conversion specifier in the format string is processed as follows:
[
, c
and n
, leading whitespace characters are skipped.
Type length specifiers
A type length specifier affects the conversion as follows:
hh
causes a d
, i
, o
, u
, x
, X
or n
format conversion to assign the converted value to an object of type signed char
or unsigned char
.
h
causes a d
, i
, o
, u
, x
, X
or n
(integer) format conversion to assign the converted value to an object of type short
or unsigned short
.
j
causes a d
, i
, o
, u
, x
, X
or n
conversion to assign the converted value to an object of type intmax_t
or uintmax_t
.
l
(“el”) causes a d
, i
, o
, u
, x
, X
or n
(integer) conversion to assign the converted value to an object of type long
or unsigned long
.
l
(“el”) causes an a
, A
, e
, E
, f
, F
, g
or G
conversion to assign the converted value to an object of type double
.
l
(“el”) causes a c
, s
or [
conversion to assign the converted value to an object of type wchar_t
.
ll
(double “el”) causes a d
, i
, o
, u
, x
, X
or n
format conversion to assign the converted value to an object of type long long
or unsigned long long
.
L
causes an a
, A
, e
, E
, f
, F
, g
or G
conversion to assign the converted value to an object of type long double
.
t
causes a d
, i
, o
, u
, x
, X
or n
conversion to assign the converted value to an object of type ptrdiff_t
or to the corresponding unsigned
type.
z
causes a d
, i
, o
, u
, x
, X
or n
conversion to assign the converted value to an object of type size_t
or to the corresponding signed integer type. Conversion type specifiers
The valid conversion type specifiers are:
a
, A
, e
, E
, f
, F
, g
or G
float
. c
NUL
character ('\0'). For a single character assignment, a pointer to a single object of type char is sufficient. l
(“el”) qualifier is present, a sequence of characters are converted from the initial shift state to wchar_t
wide characters as if by a call to mbrtowc(). The conversion state is described by a mbstate_t
object. d
10
. The argument is assumed to point to an object of type int
. i
0
. The argument is assumed to point to an object of type int
. n
int
that's pointed to by the argument. The number of items that have been scanned and assigned (the return value) isn't affected by the n
conversion type specifier. o
8
. The argument is assumed to point to an object of type int
. p
x
conversions below. The converted value is taken as a void *
and then assigned to the object pointed to by the argument. If the sequence contains more than 8
digits, the conversion is performed only on the last 8
digits in the sequence. s
char
, signed char
or unsigned char
and a terminating NUL
character, which the conversion operation adds. l
(“el”) qualifier is present, a sequence of characters are converted from the initial shift state to wchar_t
wide characters as if by a call to mbrtowc(). The conversion state is described by a mbstate_t
object. u
unsigned int
. x
, X
16
. The argument is assumed to point to an object of type unsigned
. [
NUL
character, which the conversion operation adds. l
(“el”) qualifier is present, a sequence of characters are converted from the initial shift state to wchar_t
wide characters as if by a call to mbrtowc() with mbstate set to 0
. The argument is assumed to point to the first element of a wchar_t
array of sufficient size to contain the sequence and a terminating NUL
character, which the conversion operation adds. [
and the terminating ]
. If the conversion specification starts with [^
, the scanlist matches all the characters that aren't in the scanlist. If the conversion specification starts with []
or [^]
, the ]
is included in the scanlist. (To scan for ]
only, specify %[]]
.) A
- in the scanlist that isn't the first character, nor the second where the first character is a ^
, nor the last character, defines a range of characters to be matched. This range consists of characters numerically greater than or equal to the character before the -, and numerically less than or equal to the character after the -
. %
%
character (The entire specification is %%
). 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, and the function to returns with an error.
The number of input arguments for which values were successfully scanned and stored, or EOF
if the scanning stopped by reaching the end of the input stream before storing any values.
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, skips 34.555e-3
, assigns 0xabc
to hexnum and 1234
to decnum. The return value is 3
.
The program:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main( void ){char string1[80], string2[80];memset( string1, 0, 80 );memset( string2, 0, 80 );scanf( "%[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWZ ]%*2s%[^\n]",string1, string2 );printf( "%s\n", string1 );printf( "%s\n", string2 );return (EXIT_SUCCESS);}
with input:
They may look alike, but they don't perform alike.
assigns "They may look alike" to string1, skips the comma (the "%*2s" matches only the comma; the following blank terminates that field), and assigns " but they don't perform alike." to string2.
To scan a date in the form “Friday March 26 1999”:
#include <stdio.h>#include <stdlib.h>#include <string.h>int main( void ){int day, year;char weekday[10], month[12];int retval;memset( weekday, 0, 10 );memset( month, 0, 12 );retval = scanf( "%s %s %d %d", weekday, month, &day, &year );if ( retval != 4 ){printf( "Error reading date.\n" );printf( "Format is: Friday March 26 1999\n" );return (EXIT_FAILURE);}printf( "weekday: %s\n", weekday );printf( "month: %s\n", month );printf( "day: %d\n", day );printf( "year: %d\n", year );return (EXIT_SUCCESS);}
ANSI, POSIX 1003.1
fscanf(), fwscanf() sscanf(), swscanf(), vfscanf(), vfwscanf(), vscanf(), vsscanf(), vswscanf(), vwscanf(), wscanf()
Предыдущий раздел: Описание API системной библиотеки