Grammar:
#include <stdio.h>
int scanf (const char *format, ...);
Similar functions have
int scanf (const char *format, ...);
int fscanf (FILE *stream, const char *format, ...); /Specify input stream
int sscanf (const char *STR, const char *format, ...); /designated Buffers
The scanf () function reads from stdin (standard input) according to the format specified by format, and saves the data to other parameters. It's a bit like printf (). The format string consists of control characters, white space characters, and non-whitespace characters. The control character starts with a% symbol, as follows:
Control character Description
%c a single character
%d of a decimal integer
%i an integer
%e,%f,%g a floating-point number
%o An octal number
%s A string
%x a hexadecimal number
%p A pointer
%n An integer equal to the number of characters read
%u an unsigned integer
%[] a character set
Percent of a precision symbol
1.SCANF () reads the input that matches the format string. When reading to a control character, it places the value into the next variable. Blanks (tabs, spaces, and so on) are skipped. Non-whitespace characters and input are matched and discarded. If it is a number between the% symbol and the control, then only the specified number of characters is converted into the variable. If scanf () encounters a character set (denoted by a%[] control character), any character in the parentheses is read into the variable. The return value of scanf () is the number of variables that were successfully assigned, and EOF is returned when an error occurs.
The general format of the 2.SCANF () function is: scanf ("format string", Entry first Address table)
The general form of the 3.SCANF format control is:%[*][width][f| N][h|l] Type character
The control character in [] is optional
4. "*" indicates that the input is not assigned any variable after it is read in, that is, skipping the input value.
5. "width" indicates the length of the input read-in character, and assigns the number of the corresponding width to the corresponding variable in the following list for integer type, and for the character type, the first word is assigned to the corresponding variable after the character is read into the corresponding length, and the remainder is automatically discarded. For example scanf ("-=", &a, &b), if the input is 12345 assigns 12 to a, 45 to B;SCANF (",<", &a, &b), if the input is 12345, assigns ' 1 ' to A, and ' 3 ' to B.
"%s" The entire input as a string, and set the end of '
"%ns", N is an integer, the string read into the longest not more than N, and then at the end of the "
%nf reads in a floating-point number with a maximum of n-bit integers, with more than N, truncated.
"%n[a-z]" reads up to n characters and stops if a non-a-Z character is encountered
"%[^=]" reads any number of characters until it encounters "=" Stop
"%n[^=" reads up to n characters before the "=" sign
6.F, N, H, l represent far pointers, near pointers, short integers, and long integers, respectively.
7. There are some more useful controls for the input string.
For example, often need to read into a line of strings, and this string of characters may have spaces, tabs and other white space characters, if the direct use of%s is not possible, so some people think with get (), of course, this is an option, but people who understand C basically know that gets () is a very dangerous function, and difficult to control, In particular, and scanf () alternate use of the former disadvantage is more sweeping, so get () is generally not recommended, in fact, with% [^\n] can be a good solution to this problem, ^ "non", that is, read the characters after the end of the read into. So want to read into a line of string directly with scanf ("%[^ \n]%*c", str);
The function of%*c is to read \ n, otherwise the read-in will always be \ n.
All controls that work on%s can be used with%[], such as%[0-9] to indicate that only the characters between ' 0 ' and ' 9 ' are read,%[a-za-z] is read-only in letters,
'-' is a range of connectors, of course, you can also directly list the characters you need to read into.
If you only need to read the characters in "ABC" You can use%[ABC] (or%[cab],%[ACB],%[a-c],%[c-a ...),
If you want to read a string outside a range, add a ' ^ ' in front of it, such as:%[^a-z] to indicate a character that is not read in lowercase letters.
For example, from the keyboard input "1235ab86" read 1235, 86 to N, there are the following methods:
#include <stdio.h>
BOOL Skip () {
scanf ("%*[^0-9]");
return true;
}
void Main ()
{
int n;
while (Skip () && scanf ("%d", &n)!=eof)
printf ("%d\n", N);
}
Output is: 1235
86
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.