How to Use the % [] and % n specifiers of scanf/fscanf in C

Source: Internet
Author: User

Usage of standard input/output functions % [] and % n specifiers
Scanf fscanf, read from the first non-space printable character!
The standard input/output function scanf has many conversion specifiers, which are often used as entry-level functions in various textbooks. However, [] and n are both the standard specifiers specified by c89/c99, but rarely appear in most textbooks. Although [] and n specifiers are less frequently used than other specifiers, they cannot be underestimated in programming, especially [] specifiers.
In the context of the crowd, scanf uses blank characters as the delimiters, but what if the input string uses other characters as the delimiters? [] Is the conversion specifier specially used to solve this problem. [] Conversion specifiers can generate the result character set in two ways. If the first [character has no escape character (^) on the right, the character between [] is the result character set, if the [symbol on the left side is close to an escape character (^), the character between ^ and] is the escape character, other printable characters are the result character set.

Before using the [] specifier, you must first understand two concepts: Scan list. Scanlist refers to the characters that are contained between [and], except for the escape character that is adjacent to the left [character, for example:

Scanf ("% [abcd]", ptr );

A scan list composed of abcd. Second, scan character set ). The scan character set refers to the result character set. In the preceding example, The result character set is abcd. If you enter a string "cbadkjf", the ptr string is cbad, And the kjf string belongs to the delimiters. When you enter k characters, the input string is truncated, three characters of kjf are left in stdin. For example:

Scanf ("% [^ abcd]", ptr );

The scan list is still abcd, but the scan character set is an input character other than abcd. If the input string "jksferakjjdf" is input, the ptr returns the string "jksfer ". If you want to limit the number of characters in the input string, you can use a bit field before [], as described in "s". For example:

Scanf ("% 10 [^ abcd]", ptr );

In this way, the result string can contain a maximum of 10 characters (except the '/0' character ).

The [symbol can be used as a member in the scan list. However, except for the leftmost [character or escape character, it is not considered a member of the scan list in other cases. For example, "% [] abcd]" or "% [^] abcd]". In the preceding two cases,] characters are members of the scan list, however, if it is "% [AB] cd]", the characters in the middle will not be considered as members of the scan list, and the input and output results will be messy.

For minus signs-, the-character is considered a member of the scan list only when it is closely followed by the [character or highlight character and serves as the last member of the scan list. The c standard sets the remaining circumstances as compiler-related. Most compilers define the subtraction in this case as a hyphen, for example:

Scanf ("% [a-zA-Z]", ptr );

The scan list consists of 26 uppercase and lowercase letters. A few compilers still regard the subtraction in this case as a scan List member.
Fscanf (fd, "% * [^/n]/n"); // % * is a virtual read, not saved, just let the pointer skip this variable!

% N specifies the number of valid characters output. % n can be used in both scanf and printf. The shape parameter corresponding to % n is an int type pointer. % n does not affect the return values of scanf and printf. For example:

Scanf ("% d % n", & I, & j, & k );

If the input is 434 6434, k equals 8, and scanf returns 2. Another example:

Scanf ("% c % n", & ch, & k );

After entering "sbcdetpd", k is equal to 1 rather than 8, because % c only takes one character and % n outputs the number of valid characters.

% N is used in the printf function to indicate the number of output characters, for example:

Printf ("I = % d, j = % d/n % n", I, j, & k );

If I = 343, j = 123, k = 12, and % n does not affect the return value of printf, the return value is still 12, rather than 14.

 

Related Article

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.