当前位置:网站首页>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
边栏推荐
- 【反序列化漏洞-01】序列化与反序列化简介
- RetinaNet:Focal Loss for Dense Object Detection
- Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
- [golang] golang implements the string interception function substr
- Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?
- Code of login page
- Ctfshow ThinkPHP topic 1
- [golang] golang implements the URLEncode URLDecode function
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
- 运算放大器 —— 快速复苏笔记[贰](应用篇)
猜你喜欢

如何从功能测试到自动化测试?

Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?

CSDN会员的魅力何在?我要他有什么用?

Decomposition of kubernets principle

Docker builds MySQL master-slave replication

Blue Bridge Cup provincial match training camp - Calculation of date
![[attack and defense world web] difficulty five-star 15 point advanced question: ics-07](/img/97/555a76be9e96629fd7379ce8612a3b.png)
[attack and defense world web] difficulty five-star 15 point advanced question: ics-07

Zynq TTC usage

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

This is the right way for developers to open artifact!
随机推荐
Fiddler packet capture tool summary
Blue Bridge Cup - binary conversion exercise
自动推理的逻辑06--谓词演算
08 [AIO programming]
Robot Framework官方教程(一)入门
Idea runs the wordcount program (detailed steps)
【反序列化漏洞-01】序列化与反序列化简介
stream流
MOS管 —— 快速复苏应用笔记(壹)[原理篇]
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
This should be postman, the most complete interface testing tool in the whole network
FastCGI运行原理及php-fpm参数配置
Only "a little bit", why do developers look up to you?
基于NoCode构建简历编辑器
2022, the average salary of the soft tester, after reading it, I was instantly cool
CSDN会员的魅力何在?我要他有什么用?
Introduction to kubernetes Basics
【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
[golang] golang implements the post request to send form type data function
Fastcgi operation principle and PHP FPM parameter configuration