当前位置:网站首页>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 ~
边栏推荐
猜你喜欢

Leetcode 151. 颠倒字符串中的单词

Energy principle and variational method note 14: summary + problem solving

solidworkd学习笔记:草图几何关系及编辑

关于网段CIDR的笔记

AtCoder B - Pizza

Energy principle and variational method note 12: minimum potential energy principle

Energy principle and variational method note 19: minimum complementary energy principle + possible work principle

MySQL data recovery - using the data directory

梅科尔工作室-华为14天鸿蒙设备开发实战笔记四

Edge cloud | 1. overview
随机推荐
QT With OpenGL(帧缓存篇)
2022/7/21 training summary
搭建自己的目标检测环境,模型配置,数据配置 MMdetection
Training log on July 22, 2022
Baidu map data visualization
小程序頭像組樣式
Leetcode 216. combined sum III
安装Win11找不到固态硬盘如何解决?
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
[untitled]
Uncover the working principle of solid state disk
2022/7/21训练总结
[unity project practice] level unlocking
Typescript use of new data type symbol
AtCoder B - Pizza
Leetcode 151. invert words in strings
李鸿章 临终诗 临终奏折 全文
【ASP.NET Core】选项模式的相关接口
如何合理地估算线程池大小