当前位置:网站首页>C语言库函数getchar()怎么使用
C语言库函数getchar()怎么使用
2022-07-28 13:36:00 【亿速云】
C语言库函数getchar()怎么使用
今天小编给大家分享一下C语言库函数getchar()怎么使用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
getchar()的描述以及用法
getchar()是stdio.h中的库函数,它的作用是从缓冲区中读取一个字符,如果缓冲区中没有字符可供读取,它就会等待终端键盘输入字符,按回车键表示输入结束。
* 回车键也算一个字符,放在缓冲区中(后面会在VS调试监视出给大家看)*
下面是 getchar() 函数的声明:
int getchar(void)
该函数的返回类型是int类型,返回值是用户输入字符的ASCII码值。
案例
基础的例子就不列举了,来说说今天刷C语言题目遇到的一个题目
判断输入的是否是字母(简单的题目)
int main(){ char a = '0'; scanf("%c", &a); if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z') printf("YES\n"); else printf("NO\n"); return 0;}直接判断就可以了,但是我想着可不可以实现多组输入,输入一个字符判断一个字符(虽然题目没说到)于是我就尝试写了如下代码
int main(){ char a = '0'; while (scanf("%c", &a) != EOF) { if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z') printf("YES\n"); else printf("NO\n"); } return 0;}此时运行结果如下:

运行出来为啥和我想的不一样,不应该输入一个只判断一次吗?怎么又进了一次循环并自动判断为false输入NO了?然后我开始调试。
这是输入了a之后跳出了第一次循环

然后我按F10程序继续跑了,进入了第二次循环并直接运行了else后的语句

此时我才发现变量a的值发生了变化,变成了'\n',我才意识到键盘录入按回车结束时,回车也算作一个字符'\n'被留在了缓冲区,所以进行了第二次判断字符‘\n’,则输出NO,这时候我才了解到getchar()函数在这里使用的好处了,所以我对代码进行了更改

此时程序重新运行

要想再次使用scanf就要在这里调用getchar()把缓冲区的 '\n' 给拿走,才能完成多组输入字符的判断
图文解释

因为scanf把\n前面的字符读取了,此时缓冲区还剩下'\n',所以在代码中加入一条getchar()将'\n'做清除,方便后续程序的正常进行。
以上就是“C语言库函数getchar()怎么使用”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
边栏推荐
- [translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance
- Metersphere -- Open Source continuous testing platform
- How did Dongguan Huawei cloud data center become a new model of green data center?
- 【Utils】CookieUtil
- jenkins
- Floating point data type in C language (did you learn to waste it)
- MVC模型:日历系统
- JMeter installation tutorial and login add token
- Nport serial server configuration website (whether the serial server is from network port to serial port)
- C语言中浮点数据类型(你学废了吗)
猜你喜欢
![[线程安全问题] 多线程到底可能会带来哪些风险?](/img/79/112ab7e586b0bceb296dfddb2728be.png)
[线程安全问题] 多线程到底可能会带来哪些风险?
![[ecmascript6] async and await](/img/3c/c7de42ad572dc95b188243c02dd228.png)
[ecmascript6] async and await

Leetcode 105. construct binary tree from preorder and inorder traversal sequence & 106. construct binary tree from inorder and postorder traversal sequence
![[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks](/img/23/4d07adcb5fb2c3b4c5090dacd02c89.jpg)
[server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
C# 获取当前路径7种方法

Alibaba, jd.com, Tiktok: push cloud to the heart of industry

Clickhouse architecture and design
Excel VBA 开发过程中遇到的一些问题,解决方案,持续更新

Floating point data type in C language (did you learn to waste it)

2022 safety officer-a certificate operation certificate examination question bank simulated examination platform operation
随机推荐
Career planning of Software Test Engineer
HCIP第十一天
Three cases of thread blocking.
Thesis study -- masked generative disintegration
How does vos3000 send incoming calls to okcc
Force deduction solution summary 1331 array sequence number conversion
[ecmascript6] other new interface features
天气这么热太阳能发电不得起飞喽啊?喽啊个头……
Recommended super easy-to-use mobile screen recording software
树莓派基础 | 总结记录树莓派学习过程中的一些操作
[ecmascript6] class
Thrift 序列化协议浅析
【Utils】CookieUtil
Development and definition of software testing
2022 melting welding and thermal cutting examination questions and online simulation examination
Revised version | target detection: speed and accuracy comparison (faster r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
RSA encrypts data with private key and decrypts data with public key (not a signature verification process)
468产品策划与推广方案(150份)
九、uni-popup用法 下拉框底部弹窗效果
QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样