C-SCANF consecutive calls and the problem of%c exists

Source: Internet
Author: User

Problem phenomenon:

When there are multiple scanf in the program, for the input of the first scanf, the average user ends up with whitespace characters (spaces, line breaks, tab, page break), and so on. But if there is a scanf ("%c", &ch), then the white space character just entered will be read into this scanf, so the white space character is also a character, which will cause CH to be empty.

Explanation of Reason:

How scanf Works: (In general, no other characters in the format string) any input from the user will be saved in the buffer, and when there is scanf,scanf reads the character from this buffer queue until it encounters a white space character or the character cannot be the input of this time. And put this character back in the original queue, this character becomes the first character of the queue. The next time the scanf is called, will be read from the first character, if this is the%D,%F,%G,%E data format string, you can skip the white space character, until a number is present, if this is a char (string) format string, it will be read into the blank characters , and assign a value to the variable.

In addition, the program exits unexpectedly if it is not possible to read the characters represented by this format string. such as scanf ("%d", &num), enter KK.

Workaround:

When dealing with ordinary characters in a format string, the action taken by scanf depends on whether the character is a white space character.

L White space character: when one or more contiguous white-space characters are encountered in the format string, scanf repeats the white-space character from the input until it encounters a non-whitespace character (put the character back in place). The number of whitespace characters in the format string does not matter, and a white space character in the format string can match any number (including 0) of white space characters in the input.

L Other characters: when non-whitespace characters are encountered in the format string, scanf will compare it to the next input character. If the two characters match, then scanf will discard the input characters and continue processing the format string. If the two characters do not match, then scanf will put the mismatched characters back into the input and exit unexpectedly. For example, scanf ("%d/%d,&a,&b"), input is 5/96, then a=5,b=96.

Summarize:

(1) If there are no other characters in the format string, the read-in may be all input to this variable until it is clearly not the input of this variable. A format string of non-character type (such as%d,%f,%g,%e) skips all whitespace characters looking for the first character input, but a string of character types, such as%c, does not skip whitespace characters.

(2) If there are other characters in the format string, they are divided into white space characters and non-whitespace characters.

Therefore, the above problems can be resolved using scanf ("%c", &ch).

C-SCANF consecutive calls and the problem of%c exists

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.