当前位置:网站首页>问题:先后键入字符串和字符,结果发生冲突
问题:先后键入字符串和字符,结果发生冲突
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函数; - 可以自己研究一段代码逻辑,针对间隔符做出应对措施。
参考链接
边栏推荐
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- [Yugong series] go teaching course 005 variables in July 2022
- 清华姚班程序员,网上征婚被骂?
- [shortest circuit] acwing1128 Messenger: Floyd shortest circuit
- Poor math students who once dropped out of school won the fields award this year
- R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
- [neural network] convolutional neural network CNN [including Matlab source code 1932]
- 正在運行的Kubernetes集群想要調整Pod的網段地址
- SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
- What is high cohesion and low coupling?
猜你喜欢

OneDNS助力高校行业网络安全

sql里,我想设置外键,为什么出现这个问题
![112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]](/img/b6/6dfe9be842204567096d1f4292e8e7.png)
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]

Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)

Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)

Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt

《通信软件开发与应用》课程结业报告

Talk about SOC startup (x) kernel startup pilot knowledge

Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod

MySQL安装常见报错处理大全
随机推荐
Some opinions and code implementation of Siou loss: more powerful learning for bounding box regression zhora gevorgyan
Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
Explore cloud database of cloud services together
【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
总结了200道经典的机器学习面试题(附参考答案)
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
Steps of redis installation and self startup configuration under CentOS system
Talk about SOC startup (VI) uboot startup process II
正在運行的Kubernetes集群想要調整Pod的網段地址
C#中在路径前加@的作用
[encapsulation of time format tool functions]
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
禁锢自己的因素,原来有这么多
Tsinghua Yaoban programmers, online marriage was scolded?
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
Blog moved to Zhihu
Various uses of vim are very practical. I learned and summarized them in my work
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
5V串口接3.3V单片机串口怎么搞?