当前位置:网站首页>问题:先后键入字符串和字符,结果发生冲突
问题:先后键入字符串和字符,结果发生冲突
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函数; - 可以自己研究一段代码逻辑,针对间隔符做出应对措施。
参考链接
边栏推荐
- CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
- 【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
- 正在運行的Kubernetes集群想要調整Pod的網段地址
- 大佬们有没有人遇到过 flink oracle cdc,读取一个没有更新操作的表,隔十几秒就重复读取
- Improve application security through nonce field of play integrity API
- How to add aplayer music player in blog
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
- VIM command mode and input mode switching
- R语言可视化分面图、假设检验、多变量分组t检验、可视化多变量分组分面箱图(faceting boxplot)并添加显著性水平、添加抖动数据点(jitter points)
- Use references
猜你喜欢

清华姚班程序员,网上征婚被骂?

【全栈计划 —— 编程语言之C#】基础入门知识一文懂

人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》

.NET MAUI 性能提升
![[system design] index monitoring and alarm system](/img/8e/9c4c168f7f2b8e1f0786a5fe158544.png)
[system design] index monitoring and alarm system

软件内部的定时炸弹:0-Day Log4Shell只是冰山一角

正在运行的Kubernetes集群想要调整Pod的网段地址

Flet教程之 15 GridView 基础入门(教程含源码)
![[shortest circuit] acwing1128 Messenger: Floyd shortest circuit](/img/a4/783bdcc2b97938efc77b7da6442866.png)
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
![[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]](/img/65/bf1d0f82878a49041e8c2b3a84bc15.png)
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
随机推荐
【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】
竟然有一半的人不知道 for 与 foreach 的区别???
In SQL, I want to set foreign keys. Why is this problem
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
QT | multiple windows share a prompt box class
【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
一起探索云服务之云数据库
[encapsulation of time format tool functions]
EasyUI learn to organize notes
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
Various uses of vim are very practical. I learned and summarized them in my work
Steps of redis installation and self startup configuration under CentOS system
How to write test cases for test coupons?
2022年在启牛开华泰的账户安全吗?
Talk about SOC startup (VI) uboot startup process II
Excel公式知多少?