Scanf/sscanf % [] format control string usage

Source: Internet
Author: User
Scanf is a rare but useful conversion character: [...] and [^...].
# Include <stdio. h>
Int main ()
{
Char strings [100];
Scanf ("% [1234567890]", strings );
Printf ("% s", strings );
Return 0;
}
Run. Input 1234werew and the result is 1234.
It can be found through running that if the input character belongs to a character in the string in square brackets, it is extracted; if it is not found, the extraction is completed. This method automatically adds a string terminator to the end of the extracted character.
Scanf ("% [^ 1234567890]", strings); it is used to end extraction if a character is found to belong to a character in the string within square brackets; extract this character if it does not. This method automatically adds a string terminator to the end of the extracted character.
Note: square brackets cannot contain spaces, for example, scanf ("% [1234567890]", strings); scanf ("% [^ 1234567890]", strings ); do not include spaces.
This method can also solve the problem that scanf input cannot contain spaces. Use
Scanf ("% [^/n]", strings. It's amazing.

Ansi c standard direction
Scanf () adds a new feature called scan set ). The scan set defines a character set combination, which can be composed of scanf ()
Read the allowed characters and assign them to the corresponding character array. A scan set is defined by a string of characters in square brackets. It must be prefixed with a percent sign before the left square brackets. For example, the following scan set causes scanf ()
Read characters A, B, and C:
% [ABC]

When using the scanning set, scanf () continuously eats the characters in the set and puts them in the corresponding character array until it finds that the characters are not in the Set (that is, the scanning set only reads matching characters ). When returned, the array contains a string ending with null and composed of read characters.

Character ^ can be used to describe the complement set. When the ^ character is placed as the first character of the scan set, it forms a complementary set of commands consisting of other characters, indicating that scanf () only accepts unspecified other characters.
For many implementations, a hyphen can be used to specify a range. For example, the following scan set allows scanf () to accept letters A to Z:
% [A-Z]
It is important to note that the scan set is case sensitive. Therefore, if you want to scan uppercase and lowercase characters, you must specify uppercase and lowercase letters respectively.

Collect some special usage:

 

% [] Usage: % [] indicates that a character set is to be read. If [followed by the first character is "^", it indicates the inverse meaning.

The string in [] can be 1 or more characters. The null character set (% []) is in violation of regulations and can

Result in unpredictable results. % [^] Is also in violation of regulations.

% [A-Z] reads strings between a-Z. If it is not earlier than this, it is stopped, as shown in figure

Char s [] = "Hello, my friend"; // note: the comma is between non-a-Z.

              sscanf( s, “%[a-z]”, string ) ; // string=hello

% [^ A-Z] reads strings not between a-Z. If a character between a-Z is encountered, it is stopped, as shown in figure

Char s [] = "hellokitty"; // note: the comma is not between a and Z.

              sscanf( s, “%[^a-z]”, string ) ; // string=HELLO

% * [^ =] The variable is not saved with the * sign. Skips a qualified string.

              char s[]="notepad=1.0.0.1001" ;

       char szfilename [32] = "" ;

       int i = sscanf( s, "%*[^=]", szfilename ) ;

// Szfilename = NULL because it is not saved

int i = sscanf( s, "%*[^=]=%s", szfilename ) ;

// szfilename=1.0.0.1001

% 40C reads 40 characters

% [^ =] Reads the string until '=' is reached. '^' can contain more characters, such:

              char s[]="notepad=1.0.0.1001" ;

              char szfilename [32] = "" ;

             int i = sscanf( s, "%[^=]", szfilename ) ;

           // szfilename=notepad

If the parameter format is: % [^ =:], you can also read notepad from notepad: 1.0.0.1001.

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.