当前位置:网站首页>问题:先后键入字符串和字符,结果发生冲突
问题:先后键入字符串和字符,结果发生冲突
2022-07-07 10:02:00 【CodeQingqing】
软件环境
Microsoft Visual Studio 2010
版本 10.0.40219.1 SP1Rel
问题简化
在工程中添加如下代码:
#include <stdio.h>
#define NameLength 10
int main()
{
char temp;
char name[NameLength];
printf("输入name:");
scanf("%d", name);
printf("输入temp:");
scanf("%c", &temp);
printf("name=%s\n", name);
printf("temp=%c\n", temp);
return 0;
}
运行结果如下:输入name
时,键入“李”,然后换行
Debug查看相关变量值:
通过百度ASCII码可知:temp
的值是换行符
分析
scanf
和printf
函数都是从数据流中读取数据,对于键入数据后面的间隔符(本例中是换行符),scanf
函数会将其放回输入流,即:下一次scanf
函数读取输入时,最先读取的是,上一次读取中被scanf
函数抛弃的间隔符。
这样就能够解释Debug时,temp
的值就是换行符
接下来将问题中的情况扩展,如果是以下情况,运行结果就有所不同:
- 问题中的情况是先用
scanf
函数键入字符串,后用scanf
函数键入单个字符:运行结果不正常,字符的值为间隔符。 - 先后两次用
scanf
函数键入单个字符:运行结果不正常,第二次字符的值为间隔符。 - 先后两次用
scanf
函数键入字符串:运行结果正常
建议
通过分析可以看出,不同情况下,间隔符对运行结果的影响不同。所以,作为解决问题的第一步,应该灵活使用编译器的Debug功能,通过查看相关变量的值,从而发现间隔符对最终运行结果是否有影响。
如果Debug后,发现间隔符对最终运行结果有影响,那就进入下面解决问题的第二步。
目前没有类似公式模板的代码,我试了很多demo
,也学习了参考链接里的方法,发现具体实现方法千奇百怪,但实现核心都是解决scanf
函数丢弃的间隔符。
所以,建议如下:
- 尽量不要让键入字符、键入字符串的语句前后相邻,否则很容易出现问题;
- 键入字符串用
gets
函数,键入单个字符用getchar
函数; - 可以自己研究一段代码逻辑,针对间隔符做出应对措施。
参考链接
边栏推荐
- 5V串口接3.3V单片机串口怎么搞?
- R語言使用magick包的image_mosaic函數和image_flatten函數把多張圖片堆疊在一起形成堆疊組合圖像(Stack layers on top of each other)
- Mastering the new functions of swiftui 4 weatherkit and swift charts
- Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
- 从工具升级为解决方案,有赞的新站位指向新价值
- OneDNS助力高校行业网络安全
- 竟然有一半的人不知道 for 与 foreach 的区别???
- Talk about SOC startup (VI) uboot startup process II
- Solve the problem that vscode can only open two tabs
- 相机标定(2): 单目相机标定总结
猜你喜欢
What development models did you know during the interview? Just read this one
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
Improve application security through nonce field of play integrity API
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
竟然有一半的人不知道 for 与 foreach 的区别???
HCIA复习整理
Detailed explanation of debezium architecture of debezium synchronization
La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
Flet教程之 15 GridView 基础入门(教程含源码)
随机推荐
一起探索云服务之云数据库
千人规模互联网公司研发效能成功之路
2022年在启牛开华泰的账户安全吗?
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
超标量处理器设计 姚永斌 第9章 指令执行 摘录
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
Electron adding SQLite database
相机标定(2): 单目相机标定总结
【最短路】Acwing1128信使:floyd最短路
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
一度辍学的数学差生,获得今年菲尔兹奖
深度学习秋招面试题集锦(一)
Various uses of vim are very practical. I learned and summarized them in my work
Automated testing framework
R Language Using Image of magick package Mosaic Function and Image La fonction flatten empile plusieurs images ensemble pour former des couches empilées sur chaque autre
Network protocol concept
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
Reasons for the failure of web side automation test
Enclosed please find. Net Maui's latest learning resources