Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the working principle of C language scanf

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the working principle of C language scanf". In daily operation, I believe many people have doubts about what is the working principle of C language scanf. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "what is the working principle of C language scanf"! Next, please follow the small series to learn together!

Principle explanation

Let's look at a piece of code and the results:

#include using namespace std;int main() { int a; char c; scanf("%d", &a); printf("a = %d", a); scanf("%c", &c); printf("c = %c", c);}

The code obviously has two scanf, but in the process of running, after executing the first scanf and printf, the code stops directly and does not continue to execute the next scanf. Why is this?

The following is an introduction to the buffer principle.

Line buffering: In this case, when a newline is encountered in the input and output, a true IO operation is performed. At this time, we input the characters first stored in the buffer, and so on press the carriage return key line before the actual IO operation. Typical representatives are standard input buffers (stdin) and standard output buffers (stdout).

As shown in the example above, we put the character '20\n' into the standard input buffer, enter '\n'(carriage return), the scanf function starts matching, the %d in the scanf function matches the integer 20, and then put it into the variable i, and then print out, while '\n' is still in the standard input buffer (stdin), if the second scanf function is scanf("%d",&i), then it will still block, Because scanf ignores characters such as '\n'(carriage return) and spaces when reading integers, floating-point numbers, and strings (strings will be explained later when arrays are introduced)(ignoring means that scanf will delete these characters first and then block), scanf will delete the corresponding characters in the buffer when it matches a character. Because no characters are ignored when executing the scanf("%c",&c) statement, scanf("%c",&c) reads '\n' that is still in the buffer.

Scanf receives %c, which treats the '\n' that still exists in the buffer as a character, causing the code to end. If scanf receives other types of data, it will ignore this '\n' and continue running the following code. For another example:

#include using namespace std;int main() { int a; int c; scanf("%d", &a); printf("a = %d", a); scanf("%d", &c); printf("c = %d", c);}

For example, the above code, I entered a lot of spaces, but it does not affect the actual running results, because they are deleted by printf in the buffer, scanf is not deleted the contents of the buffer.

Let's look at a code to understand:

#include using namespace std;#define EOF (-1)int main() { int i; while (scanf("%d", &i) != EOF) { printf("i=%d\n", i); }}

The above scanf input, is 10, 20, a sequence input, after entering a, the code has been printed on a printf content, this is because: scanf returns is successfully read data items, in my input entered a, a is unable to match %d, scanf will not delete a, so the return value of scanf is 0 (no successful match), not equal to-1, this time will always while loop.

Also, if scanf returns a value of 0, the value of i is not read, and the value of i is still the last input of 20, which causes the while loop to print the last i=20.

solutions

Empty the buffer using rewind(stdin):

#include using namespace std;#define EOF (-1)int main() { int i; while (rewind(stdin), scanf("%d", &i) != EOF) { printf("i=%d\n", i); }}

At this point, the study of "what is the working principle of C language scanf" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report