In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, the editor will show you how to use getchar () in the C language. The knowledge points in the article are introduced in great detail. Friends who feel helpful can browse the content of the article with the editor, hoping to help more friends who want to solve this problem to find the answer to the problem. Let's follow the editor to learn more about "how to use getchar () in C language".
Preface
Recently, when I was relearning the C language, I found a serious problem that I could no longer use getchar. It's not that I can't use it. I found a problem that puzzled me very much. Maybe I didn't make this concept clear when I first came into contact with the C language, so that I don't understand it now.
The point of getchar's confusion
I use the getchar function to enter a string of characters ABCD, and then give this string of characters to ch, and then I print this ch, and then I get a character A, and I see that there is no problem here, right? of course, there is no problem, because getchar can only read one character, naturally read only A, and then naturally print out only this A.
All right, let's move on.
Seeing this, I don't know if you have thought of a question. Anyway, it was because of this problem that I was confused for a long time.
First, through the first example, we know that getchar can only read one character.
But in this example, I typed a string of characters ABCDE, which is supposed to read only one character, that is, the first character An in this string, and then read it and gave it to the variable ch, and then judged that ch and so on are not equal to EOF.
EOF is the end mark of the file. On the keyboard, that is, ctrl+z, you can print it yourself and check it against the ASCII code.
Obviously, there is no ctrl+z in the current one, so the condition is true, and then execute the print statement.
According to our analysis above, then we should print a character A, right? something confusing happened, it printed ABCDEbasket characters! Yes, it's completely printed out.
Maybe you often see this similar program, and then you get used to it, and you all think it should be printed out completely.
OK, at this point, do you feel very confused? why is this happening?
Well, I'm going to tell you a little bit about it now. It turns out there's a concept involved.
Buffer zone
A buffer, also known as a cache, is part of the memory space. In other words, a certain amount of storage space is reserved in the memory space, which is used to buffer the input or output data, and this part of the reserved space is called buffer.
So what does this have to do with our getchar?
Of course, there is a connection. When we call the getchar function, this is actually done internally:
At this time, he will first read it from the cache, and after reading it, he will find that there is no data in the cache, he will wait for external input, and the cursor will flash at this time.
When we enter the characters externally, then we will hit enter to finish, and then all the characters we enter will actually be directly stored in the cache.
Suppose I type the character ABCD at this time, and then enter ends, and then there is ABCD in the cache\ n
Attention, something interesting is coming, and\ n? Ha. Keep looking down.
Problems caused by buffers
Let's take a closer look at it and see why I didn't enter characters in getchar.
In fact, it is not that I did not enter characters, but after I finished typing scanf characters, enter to end the scanf input, and then strange things happen, the program is directly over, yes, skipped the process of getchar input characters.
Do you think it's strange?
This is what I mentioned to you just now.
The thing is, when I called the scanf function at this time, I typed ABCD externally, and then after typing ABCD, I'm going to end the input, and then we'll hit enter, pay attention! This carriage return is actually a character, and then it will also be put in the cache along with the ABCD I entered earlier, that is to say, there will be ABCD in the cache\ n
Let's just do a little test and find out.
See the printed result, do you understand?
How getchar works
It turns out that when the getchar function is called, it is actually read directly from the cache. If there is data in the cache, he will directly take the first one, and then his mission will be over. In fact, here, uh, the above scanf takes the ABCD from the cache, and then there is a 10 left. This 10 is actually\ nThe representation in the ASCII code. So the rest is saved in the cache, and then he encounters the getchar function, and then getchar reads it in the cache and finds: Oh, there's one left, so I'll just take it, and then his mission will be over, so there will be a situation where he won't wait for our external input.
So can we solve this problem?
Empty the cache to solve the problems caused by the buffer
After understanding the connection between buffer and getchar, it will be easy for us to solve it.
We just need to insert a getchar function before the getchar read, that is to say, let's use a getchar function to read the rest of the cache\ n before the next real getchar read, so that the next time we actually read the getchar, the cache will be empty.
Well, we fully understand how getchar works, so let's go back to the original confusion.
Solve the initial confusion
First loop: first the cache is empty, the cursor flashes, and then wait for my input
I typed a character A, and then I continued to enter the end of the carriage return, so I actually typed two characters, one character An and one character\ n, and they were all stored in the cache. Then after the cache has the data, the cursor is not flashing at this time, and the getchar function starts reading directly, and then in this loop, the getchar function reads a character A, gives it to ch, and then the conditional expression is true (not equal to ctrl+z), so print ch is executed, and then a character An is printed out.
Second loop: at this time, there is still one left in the cache, the cursor does not flash, and getchar does not wait for external input.
Then getchar reads\ n directly into the cache\ n, continues to ch, and then the expression is still true, so continue to print the ch. At this time, because the ch has been replaced, the print line breaks and the cursor position changes.
The third loop: the cache is empty, the getchar cannot read the data, the cursor flashes, and the getchar waits for external input data.
So far, we're going to move on.
The first loop: the cache is empty and getchar cannot read the data from the cache, so the cursor flashes and the getchar waits for external input.
At this point, I enter a string of ABC, and then enter to end the input. The character ABCD\ n is in the cache, and then getchar reads the first character An and gives it to ch, then the conditional expression is true, ch is printed, and the first A comes out.
The second loop: there is still BC left in the cache\ nGetchar read directly.
At this point, getchar reads the character B, then gives it to ch, the conditional expression is true, continues to print ch, and then you get AB.
The third cycle: the cache still has C\ nDie getchar to read directly.
And then again, getchar reads to C, then gives it to ch, then the expression is true, prints out ch, and then gets ABC after this loop.
The fourth loop: there is one left in the cache, and finally print this\ n, so the cursor is next line.
The end result is ABC.
Then I type ctrl+Z again
So ctrl+z (EOF) exists in the cache, then getchar reads it, then gives it to ch, and then the expression determines that the condition is false, ch==EOF, so the loop jumps out and the program ends.
The getchar function reads data directly from the cache. If there is data in the cache, it reads the first data directly. If there is no data, then the cursor flashes and waits for external data input.
It is very important to note that the carriage return entered elsewhere will also be stored in the cache as a character, and then getchar will read it directly, which is easy to make mistakes in this place!
Thank you for your reading. The above is the whole content of "how to use getchar () in C language". Friends who learn it, hurry up and get started. I believe that the editor will certainly bring you better quality articles. Thank you for your support to the website!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.