当前位置:网站首页>Use of getchar
Use of getchar
2022-07-24 11:22:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
1. Read a character from the buffer , It's equivalent to clearing the buffer 2. Ahead scanf() When reading input, a character is left in the buffer ’\n’( End of input s[i] Press enter after the value of ), So if you don't add one here getchar() If you take this carriage return ,gets() You won't wait to type characters from the keyboard , But will take this directly “ Useless ” A carriage return , This leads to incorrect reading 3. getchar() Is to read a character in order in the input buffer ( Including Spaces 、 Return and Tab) getchar() Inconvenient to use , resolvent : (1) Use the following statement to clear carriage returns : while(getchar()!=’\n’); (2) use getche() or getch() Instead of getchar(), Its function is to read a character from the keyboard ( Don't press enter ), Note to include the header file <conio.h> 4. getchar() yes stdio.h Library functions in , Its function is from stdin Read a character into the stream , in other words , If stdin If there is data, you can directly Read out , for the first time getchar() when , It really needs manual input , But if you type more than one character , After the getchar() When executing again, it will read directly from the buffer Take it . It's actually input device -> Memory buffer -> Program getchar The key you pressed is put into the buffer , Then for the program getchar Have you ever tried to press and hold a lot of keys and then it will sound a little later , The buffer is full , The key you first pressed is not stored in the buffer . The characters entered by the keyboard are stored in the buffer , Once you type enter ,getchar Enter the buffer to read characters , Only the first character is returned at a time as getchar The value of a function , If There are cycles or enough getchar sentence , All characters in the buffer will be read out in turn until ’\n’. To understand that , The reason why a series of characters you enter are read out in turn Come on , Because of the function of circulation, it makes repeated use getchar Read characters in buffer , instead of getchar Can read multiple characters , in fact getchar Only one can be read at a time Characters . If you need to cancel ’\n’ Influence , It can be used getchar(); To get rid of , here getchar(); Just achieved ’\n’ But it is not assigned to any character variables , So don't Will have an impact , It is equivalent to clearing this character . Also note that here you enter... On the keyboard ssss The echo you see comes from getchar The role of , If you use getch Just can't see
To what you entered .
1、 First , Let's take a look at this code :
Its simple meaning is to read a character from the keyboard , Then output to the screen . Taken for granted , We type in 1, Output is 1, Input 2, Output is 2.
So if we output 12 Well ?
Its output is 1.
Here we first explain briefly , Because when we input characters from the keyboard ‘1’,‘2’, And press enter , Our input is put into the input buffer , This is the time getchar() Will read the input we just made from the buffer , Read only one character at a time , So the characters 1 It was taken out , Assigned to c, then putchar() And will be c On the standard output , That's the screen here , So we see the final display result 1. At the same time, characters ‘1’ Also released by buffer , And characters ‘2’ Still left in the buffer . And this is very unsafe , It is possible to use it next time , Our buffer will read some garbage , But when the program ends , It will refresh automatically .
2、 Let's move on to the following code :
What do you think these lines of code mean ? Do you really understand it ?
If you think you understand , Then let me ask a question , Do you think if you input a character from the keyboard, you will output a character , Until I met the line break ?
emmmmm.
If you really think so , Then you're wrong .
Its real operation process should be like this : Remember what we just said ,getchar() Will read from the input buffer , In other words, we input all the contents and press Enter Post key , Our input is sent into the input buffer , This is the time ,while The cycle begins to work , every time getchar() Read a character from the input buffer , Then if it's not a newline character, output .
3、 So why should we use buffers ?
for instance , We know that computers CPU The processing speed is very fast , And our keyboard input speed is always better than CPU Processing speed of , that CPU You have to wait until the keyboard is finished , It's a waste of resources . therefore , Our party keyboard is finished , let CPU One-off treatment , This will greatly improve efficiency .
And such as , Our printer prints documents , The processing speed of the printer is very slow , So we will output the document to the printer's cache , So the printer can print slowly by itself , Without having to occupy CPU resources .
4、 The type of buffer
buffer There are three types : Full buffer 、 Line buffered and unbuffered .
1、 Full buffer
under these circumstances , When you fill the standard I/O After cache Just entered Line actual I/O operation . A typical example of full buffering is the reading and writing of disk files .
2、 The line buffer
under these circumstances , When line breaks are encountered in input and output , Carry out the real I/O operation . At this time , The characters we input are stored in the buffer first , Wait until you press enter to wrap the line I/O operation . A typical example is keyboard input .
3、 Without buffer
That is, no buffering , Standard errors stderr It's a typical representative , This allows the error message to be displayed as quickly as possible .
5、 Buffer refresh
The buffer will be flushed in the following three cases :
1、 Buffer full
2、 perform flush The statement that flushes the buffer
3、 The program ends normally .
First , use getchar() Function to input characters , It is not to read the input characters directly from the hardware of the keyboard , But from “ Input buffer ” Characters obtained in . The input buffer is a one character queue , It stores all the characters you haven't read yet . Every time you call getchar function , It will read the first character from the input buffer , And clear this character from the input buffer . However , The design of this input buffer , Is to put everything entered from the keyboard , Including the carriage return you press every time ‘\n’, and getchar The function reads only the character you entered before the carriage return , The carriage return is kept in the input buffer . therefore , Second call getchar when , The function reads from the input buffer ’\n’. To solve this problem , There are two possible ways . One is to add one more getchar(), Filter out carriage return , But this method has shortcomings , If you're calling the first getchar More than one character was entered during the , that , Add a getchar You can't filter all unread characters . If your intention is to start again from “ keyboard ” Read it , It's better to add one fflush(stdin); Clear input buffer
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/125274.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢

这才是开发者神器正确的打开方式!

《Nature》论文插图复刻第3期—面积图(Part2-100)

Installing Oracle Xe with Linux

Idea background image set

Lanqiao cup provincial training camp - commonly used STL

Neo4j installation tutorial

This is the right way for developers to open artifact!

Docker builds MySQL master-slave replication

【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结

Performance test summary (I) -- basic theory
随机推荐
Blue Bridge Cup provincial match training camp - Calculation of date
How to use SSH and SFTP protocols at home
Text message verification of web crawler
Druid encryption command
[golang] golang implements MD5 encryption function
Redis 100 million level data storage scheme hash slot partition
视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
性能测试总结(一)---基础理论篇
08 [AIO programming]
This should be postman, the most complete interface testing tool in the whole network
Sentinel vs Hystrix 限流对比,到底怎么选?
黑马瑞吉外卖之员工信息分页查询
Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]
[deserialization vulnerability-01] Introduction to serialization and deserialization
Fiddler packet capture tool summary
Docker installs 3 master and 3 slave redis clusters
【Golang】golang实现urlencode urldecode函数
JMeter if controller
Sorting out the ideas of data processing received by TCP server, and the note of select: invalid argument error
Fifty lectures of Euler (I)