C-language scanf skipped issues

Source: Internet
Author: User

When you run the scanf function, scanf is often skipped. This is because when you have done the scanf function, in the second scanf function, because the buffer is still stored in the first end of the carriage return, the second time did not read into the carriage return directly into the, so there is no real read into the data role. At this point we can clear the buffer, and here we can use the Fflush function. Let's take a look at the Fflush function:

The flow in the graph is the data transmission in the computer (reading and writing, network connection transmission) is abstracted as a stream, the transmission of data is like flowing water, flow from one container to another. The flow is generally divided into 3 types, standard input (stdin), standard output (stdout), and error Output (stderr). After understanding the function and the flow, let's look at an example,

Why is the int type I input 123CV? Because our default user is unfriendly, no one knows what the user will do. As you can see, the "C" of the buffer is directly assigned to the B variable. Here you can think of the second scanf function being skipped, of course scanf is skipped more than this one, to see the result after adding the Fflush function:

Joined the Fflush no problem!

But there is another way to use GetChar (), but GetChar can only skip one character,

As you can see, the character "C" is skipped.

So in the programming time encountered scanf was skipped or with the Fflush function is better.

C-language scanf skipped issues

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.