< turn >scanf format detailed

Source: Internet
Author: User

scanf format control in full format:


% * M L or H format character
① format characters are used in the printf function in the same way as%d,%o,%x,%c,%s,%f,%e, no%u format, and%g format.
② can specify the width of the input data, and the system automatically intercepts the data as it is needed. Such as:
scanf ("%3d%3d", &a, &b);
Input: 123456
The system automatically assigns 123 to a,456 B.
The "*" appended specifier after ③% is used to indicate that the corresponding data is skipped. For example:
scanf ("%2d%*3d%2d", &a, &b);
If you enter the following information: 1234567. Assign "12" to A, "67" to B. The second data "345" is skipped and is not assigned to any variables.
④ cannot specify accuracy when entering data, for example:
scanf ("%7.2f", &a);
is illegal, cannot attempt to enter: 12345.67 and the value of a is 12345.67.

Additional Format Description characters:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Character description
——————————————————————————————————————
L for input of Long Integer data (available%LD,%LO,%LX) and double type data (with%LF or%le)
——————————————————————————————————————
h for entering short-integer data (available%hd,%ho,%HX)
——————————————————————————————————————
Field width Specifies the width (number of columns) of the input data, and the field width should be a positive integer
——————————————————————————————————————
* Indicates that this entry is not assigned the corresponding variable after it is read into
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
------------------Related-------------------
Input data stream delimited
① gets the data from the input stream based on the meaning of the format character, which is considered to be the end of the input stream when the data type is inconsistent with the format character requirement. For example:
scanf ("%d%c%f", &a, &b, &c);
If you enter the following information:
1234r1234.567
Then the scanf function found the "R" Type mismatch when receiving the data, so the "1234" is converted to an integer assignment to a, the "R" is assigned to the variable B, and the "1234.567" conversion narimi type data is assigned to C.
② separates data items according to the field width specified in the format item. such as statements:
scanf ("%2d%3f%4f", &a, &b, &c);
If you enter the following information:
123456789012345
The SCANF function assigns a value of 12 to the a,345 assignment to C when it receives the data, assigning the value to the b,6789 according to the field width.
③ an implicit delimiter. Spaces, jump Geff (' \ t '), newline characters (' \ n ') are data delimiters that are recognized by the C language.
④ Displays the separator character. There is one or more ordinary characters between the two format descriptions of the scanf function, and when the data is entered, it must be separated by one or more characters between two data. such as statements:
scanf ("A=%d,b=%f,c=%f", &a, &b, &c);
Then the input data should be:
A=1234,b=67.8,c=98.123

---------------------------------------
Further instructions on the scanf function:
The "format control" in the ①SCANF function should be followed by the variable address, not the variable name. For example, if A and B are integer variables, the
scanf ("%d,%d", A, b);
Is wrong, "A, B" should be replaced by "&a, &b".
② If there are other characters in the format control string other than the format description, you should enter the same characters as those characters when entering data. For example:
scanf ("%d,%d", &a, &b);
Input should be entered as: 3, 4. A comma between 3 and 4 should correspond to a comma in the "format control" in the scanf function, and it is wrong to enter other symbols.
③ the space character and the escape character nonalphanumeric as valid characters when entering characters in the format "%c".
scanf ("%c%c%c", &c1, &C2, &C3);
As input: a b C. The character ' A ' is assigned to C1, and the character (space) ' is assigned to C2, and the character ' B ' is assigned to C3.
The u specifier is not used in ④scanf. Type the unsigned data with the D, O, and x descriptors.

Add:

for const char* p = "12232114687abc12356";
scanf ("%[123]", buf); Read the word ' 1 ' or ' 2 ' or ' 3 ' into buf until it encounters a character that is not ' 1 ' and is not ' 2 ', so the BUF should be "3" after execution;
%[123] equivalent to%[231], equal to%[321] ..., the order in the list is irrelevant;
%[123] is also equivalent to%[1-3] or%[3-1], that is, "1 to 3", for continuous word list characters This is very simple to write, such as%[a-z] equivalent to%[ABC ... Omit ... z];
Think about it,%[3-14] should be equated with what? Is it "3 to 14"? Of course not, because [] is a character, not a number, so%[3-14] should be equivalent to%[3214], equivalent to%[1234];
Similarly, want to take only the letter, that can write%[a-za-z];
If the first letter of the list is ^, then just the opposite, like%[^a-za-z] means to take all characters except the letter.
For the string "abDEc123" if you want to read in letters and numbers to two strings should be "%[a-za-z]%[0-9", buf1,buf2;
What if I want to take a line of characters? "%s" is not possible, because%s encountered white space characters (space, tab, \ r, \ n) is over, so can be written as "%[^\n]%*c",%[^\n] The role just said, is read \ n All characters, that is to say, the role of%*c is to remove \ n, Otherwise read the time has been encountered is \ n;
All controls that work on%s can be used for%[], such as "%*[^\n]%*c" means skipping a line, "%-20[^\n" means reading \ n the first 20 characters.

< turn >scanf format detailed

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.