Full format of the scanf Controller

Source: Internet
Author: User

Full format of scanf format control:


% * M L or H characters
① The format characters are used in the same way as those in the printf function, in the format of % d, % O, % x, % C, % s, % F, % E, no % u format or % G format.
② You can specify the column width occupied by the input data, and the system automatically intercepts the required data according to it. For example:
Scanf ("% 3d % 3d", & A, & B );
Input: 123456
The system automatically assigns 123 to A and 456 to B.
③ "*" After % is used to indicate that the corresponding data is skipped. For example:
Scanf ("% 2D % * 3D % 2D", & A, & B );
If the following information is entered: 1234567. Assign "12" to a and "67" to B. The second "345" data is skipped and is not assigned to any variable.
④ Precision cannot be specified when data is input, for example:
Scanf ("% 7.2f", & );
It is invalid. You cannot enter 12345.67 to set the value of a to 12345.67.

Additional format description characters:
Please refer to the following link for more information:
Character Description
--------------------------------------
L used to input long integer data (available % lD, % Lo, % lx) and double data (using % lf or % Le)
--------------------------------------
H is used to input short integer data (% HD, % ho, % HX are available)
--------------------------------------
The field width specifies the width (number of columns) of the input data. The field width must be a positive integer.
--------------------------------------
* Indicates that no variable is assigned to this input item after reading it.
Please refer to the following link for more information:
-------------------
Separate input data streams
① Obtain data from the input stream according to the meaning of the format character. When the data type in the input stream does not meet the format character requirements, this item is deemed to end. For example:
Scanf ("% d % C % F", & A, & B, & C );
If you enter the following information:
1234r1234. 567
The scanf function finds that the "r" type does not match when receiving data. Therefore, it converts "1234" to an integer and assigns it to a. It assigns "R" to variable B, finally, "1234.567" is converted into real data and assigned to C.
② Separate data items based on the specified field width in the format item. Statement:
Scanf ("% 2D % 3f % 4f", & A, & B, & C );
If you enter the following information:
123456789012345
When receiving data, the scanf function assigns 12 values to a, 345 to B, and 6789 to c Based on the field width.
③ Implicit separator. Space, grid ('\ t'), line feed (' \ n') are data delimiters recognized by C.
④ Display the separator. The two format description items of the scanf function have one or more common characters. Therefore, when entering data, the two data items must be separated by one or more characters. Statement:
Scanf ("A = % d, B = % F, c = % F", & A, & B, & C );
The input data should be:
A = 1234, B = 67.8, c = 98.123

---------------------------------------
Further description of scanf functions:
① In the scanf function, "format control" should be followed by the variable address, rather than the variable name. For example, if a and B are integer variables
Scanf ("% d, % d", a, B );
No. Change "A, B" to "& A, & B ".
② If there are other characters besides the format description in the "format control" string, you should enter the same characters as these characters when entering data. For example:
Scanf ("% d, % d", & A, & B );
Enter: 3, 4. The comma between 3 and 4 should correspond to the comma in "format control" in the scanf function. It is incorrect to enter other symbols.
③ When the characters are entered in "% C" format, both space characters and escape characters are used as valid characters.
Scanf ("% C", & C1, & C2, & C3 );
For example, enter a B c. The character 'a' is assigned to C1, the character (Space) ''is assigned to C2, and the character 'B' is assigned to C3.
④ The U specifier is not used in scanf. The unsigned data is input using the D, O, and X specifiers.

Supplement:

For const char * P = "12232114687abc12356 ";
Scanf ("% [123]", Buf); // read the word '1', '2', or '3' to the Buf, until a character is not '1', not '2', and not '3' is encountered, the Buf should be "1223211" after execution ";
% [123] is equivalent to % [231], equivalent to % [321]……, The order in the list does not matter;
% [123] is equivalent to % [1-3] or % [3-1], that is, "1 to 3". For a continuous character list, writing is very simple, for example, % [A-Z] is equivalent to % [ABC... Omitted... Z];
Think about it. What should % [3-14] be equivalent? Is it "3 to 14? Of course not. Because [] is a character rather than a number, % [3-14] should be equivalent to % [3214], equivalent to % [1234];
Similarly, if you want to write only letters, you can write them as % [A-Za-Z];
If the first letter in the list is ^, the opposite is true. For example, % [^ A-Za-Z] means to take all the characters other than the letter.
For the string "abdec123", if you want to read the two strings by letter or number, it should be "% [A-Za-Z] % [0-9]", buf1, buf2;
What if I want to take a line of characters? "% S" is not acceptable, because % s encounters a blank character (space, tab, \ r, \ n) and ends, therefore, you can write "% [^ \ n] % * C", % [^ \ n], which is to read all characters other than \ n, that is to say, until \ n is read, % * C is used to remove \ n, otherwise \ n is always encountered during the next read;
All controls that take effect on % s can be used for % []. For example, "% * [^ \ n] % * C" indicates that a row is skipped, "%-20 [^ \ n]" indicates to read the first 20 characters of \ n.

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.