当前位置:网站首页>问题:先后键入字符串和字符,结果发生冲突
问题:先后键入字符串和字符,结果发生冲突
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
函数; - 可以自己研究一段代码逻辑,针对间隔符做出应对措施。
参考链接
边栏推荐
- Suggestions on one-stop development of testing life
- 【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】
- 5V串口接3.3V单片机串口怎么搞?
- Use references
- Half of the people don't know the difference between for and foreach???
- [full stack plan - programming language C] basic introductory knowledge
- Electron adding SQLite database
- 【滤波跟踪】基于matlab扩展卡尔曼滤波EKF和无迹卡尔曼滤波UKF比较【含Matlab源码 1933期】
- [filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
- Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt
猜你喜欢
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
【系统设计】指标监控和告警系统
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
竟然有一半的人不知道 for 与 foreach 的区别???
Talk about SOC startup (VII) uboot startup process III
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
What development models did you know during the interview? Just read this one
Test the foundation of development, and teach you to prepare for a fully functional web platform environment
随机推荐
Reasons for the failure of web side automation test
Electron adding SQLite database
Excel公式知多少?
Summed up 200 Classic machine learning interview questions (with reference answers)
[encapsulation of time format tool functions]
Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
正在运行的Kubernetes集群想要调整Pod的网段地址
Improve application security through nonce field of play integrity API
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
QT | multiple windows share a prompt box class
Network protocol concept
Electron adding SQLite database
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
The function of adding @ before the path in C #
Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
Onedns helps college industry network security
通过环境变量将 Pod 信息呈现给容器
Suggestions on one-stop development of testing life
Design intelligent weighing system based on Huawei cloud IOT (STM32)
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts