当前位置:网站首页>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
边栏推荐
- No one knows what ingredients tiktok's latest popular product pink sauce contains
- [golang] golang implements the post request to send form type data function
- 08【AIO编程】
- Kubernetes Foundation
- MySQL query field matches the record of a rule
- [golang] golang implements MD5 encryption function
- MySQL根据备注查询表、字段
- Selenium automated test (this one is enough) - self study
- Installing MySQL under Linux
- High frequency written test questions (Weilai)
猜你喜欢

Redis 100 million level data storage scheme hash slot partition

No one knows what ingredients tiktok's latest popular product pink sauce contains

Ldr6028 charging OTG live line live sound card audio adapter is the most cost-effective solution

MicroBlaze adds a custom IP core and attaches the Axi bus to realize ssd1306 OELD drive

网络爬虫之短信验证

Imeta view | is short reading long amplicon sequencing applicable to the prediction of microbiome function?

Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform

JMeter runtime controller

只会“点点点”,凭什么让开发看得起你?

Depth first search and breadth first search of Graphs
随机推荐
[golang] golang implements simple Memcache
08 [AIO programming]
The U.S. Department of Homeland Security launched an investigation into the electronic communication records deleted by the secret service during the riots in the Capitol
selenium3自动化测试(这一篇就够了)——自学篇
What is the charm of CSDN members? What's the use of him?
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
[golang] golang implements the post request to send form type data function
黑马瑞吉外卖之员工信息分页查询
【Golang】golang中time类型的before方法
【10】团队协作和跨团队协作
Kubernetes Foundation
E2PROM read / write (xiicps) on PS side of zcu102 board
Jmeter-Runtime控制器
Robot framework official tutorial (I) getting started
UNIX C language POSIX mutex thread synchronization
[deserialization vulnerability-01] Introduction to serialization and deserialization
这才是开发者神器正确的打开方式!
Idea background image set
MySQL queries tables and fields according to comments
视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?