当前位置:网站首页>Discussion on the usage of scanf () and getchar ()
Discussion on the usage of scanf () and getchar ()
2022-07-23 20:16:00 【Cactus will also bloom】
First let's understand scanf and getchar Usage of .
getchar()
getchar() yes stdio.h Library functions in , Its function is to read a character from the buffer , in other words , If there is data in the buffer, you can read it directly without entering it , First call getchar() when , It really needs manual input , But if you type more than one character , After the getchar() When it is executed again, it will read directly from the buffer
And scanf Different ,getchar() Able to read spaces 、 Return, etc . and scanf When encountering space and carriage return , End reading .
commonly scanf After reading the input data , Will stay like \n Etc , And if we need to call again scanf Words , There are also \n, here scanf Don't wait for us to enter data , Just put \n Read away , So we can't achieve our goal .
therefore , In the following code to judge whether you input letters , Need to use getchar() To accept the space put into the buffer or carriage return , Otherwise ,scanf The next time you accept a space or enter , Instead of the characters you entered

That's besides using getchar() Receive spaces or carriage returns in the buffer , What else can I do ?
Another way is , stay %c Preceded by a space , It means to skip all white space characters before the next character .

meanwhile , You may have questions about the following code , Obviously, it is also a circular method of input , Why in front while You need to use getchar() To receive white space characters , And below for There is no need to write getchar Well ?

By consulting Lao Chen , I know that the original space is to distinguish between numbers ,%c Can't , in other words %c Will save spaces , but %d+ When there are spaces , Spaces are not in the cache ~.
Conclusion : This is today's sharing , Thank Lao Chen for giving me a feeling of enlightenment ~
I am cactus , Sophomores at Xiamen Software Institute , If this article helps you , Can you give me a one button three connection , Pass this article to more people who need him ~
边栏推荐
猜你喜欢

The numerical sequence caused by the PostgreSQL sequence cache parameter is discontinuous with interval gap

使用Jmeter和VisualVW进行压测准备
![[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计](/img/98/8b7a4fc3f9ef9b7e16c63a8c225b02.png)
[激光器原理与应用-8]: 激光器电路的电磁兼容性EMC设计

QT下assimp库的模型加载

Osgearth uses sundog's Triton ocean and silverlining cloud effects

TASK03|回归

web安全入门-ssh测试与防御

【无标题】

Introduction to web security SSH testing and defense
![Relevant interfaces of [asp.net core] option mode](/img/2e/847e7541cfc49fd69794089dce2df2.jpg)
Relevant interfaces of [asp.net core] option mode
随机推荐
2022/7/22 训练日志
[interview: concurrent Article 22 multithreading: reentrantlock]
Leetcode - the nearest sum of three numbers
Leetcode 209. 长度最小的子数组
C language leak detection and filling (1)
梅科尔工作室-华为14天鸿蒙设备开发实战笔记五
Training log on July 22, 2022
能量原理与变分法笔记17:广义变分原理(识别因子方法)
What if there is no word document in win11? There is no word document solution tutorial in win11
task03笔记2
2022/7/21 training summary
web安全入门-ssh测试与防御
能量原理与变分法笔记14:总结+问题的求解
Osgearth uses sundog's Triton ocean and silverlining cloud effects
[unity project practice] level unlocking
Debian | Can’t locate Debian/Debhelper/Sequence/germinate.pm in @INC
ant接口用什么天线_电视后面有个接口写的是 标准ANT 75 欧输入,请问是什么意思,是天线吗?可不可以接闭路线啊?…「建议收藏」
Atelier macoll - notes de développement de la secte de l'ours 2
梅科尔工作室-小熊派开发笔记2
小程序頭像組樣式