Simple clear buffer

Table of contents

Foreword:

1. Multiple sets of input:

1.1 Some insights into the scanf function:

1.2 Multiple sets of input:

2. Clean up the buffer: 

2.1 What is a buffer:

2.2 Why introduce buffers?

2.3 Introducing the getchar function:

2.4 Simulate the environment to clean the buffer:

Summarize:

Foreword:

       When learning loop statements, there are several examples: 1. Multiple sets of input; 2. Cleaning up the buffer. I hope to be able to combine the while loop with practical problems.

1. Multiple sets of input:

1.1 Some insights into the scanf function:

       Before explaining multiple sets of inputs, we need to introduce the scanf function, which is detailed in the link below. https://legacy.cplusplus.com/reference/cstdio/scanf/?kw=scanf , the following is my own opinion:

       1. The return value of the scanf function is of int type and returns the number of read data . The following code is used to prove:

       2. When the scanf function reads a string, it stops reading when it encounters a space.

Summary: The scanf function reads from the data in the buffer. After the reading is successful, it returns the number of read data; after the reading fails, it returns EOF (-1); secondly, when reading the string, it encounters Spaces are no longer read.

1.2 Multiple sets of input:

Let’s take a simple question:

2. Clean up the buffer: 

2.1 What is a buffer:

       The buffer is a portion of the memory space . In other words, a certain amount of storage space is reserved in the memory space. This storage space is used to buffer input or output data. This reserved space is called a buffer. The buffer is divided into an input buffer and an output buffer according to whether it corresponds to an input device or an output device .

2.2 Why introduce buffers?

       In the past, we had to use disks to store information. We could first put the data in the buffer , and then read it from the disk after the data in the buffer was read. This reduces the number of reads and writes to the disk. In addition, the computer's operations on the buffer are much faster than those on the disk. Therefore, applying the buffer can greatly improve the running speed of the computer.

2.3 Introducing the getchar function:

       As the name suggests, the function of the getchar() function is to obtain a character , while the function of the putchar() function is to output a character .

      Next, let’s introduce the specific function of getchar() function: there is a detailed introduction on the link below. https://legacy.cplusplus.com/reference/cstdio/getchar/?kw=getchar . The getchar() function returns the ASCII value of the character read after successful reading; returns EOF (-1) after failed reading or when the end of the file is encountered.

2.4 Simulate the environment to clean the buffer:

       Use a loop to clean the buffer, so that if there are special circumstances, we can also clear the buffer.

while ((ch = getchar()) != '\n')
		;

Summarize:

       The editor believes that these two small steps may become a step in a certain problem, and they still need to be studied. Secondly, through this problem, we can further understand the concept of buffer zone. I hope you guys can comment.

おすすめ

転載: blog.csdn.net/2301_77868664/article/details/131763802