当前位置:网站首页>Problem: the string and characters are typed successively, and the results conflict
Problem: the string and characters are typed successively, and the results conflict
2022-07-07 11:59:00 【CodeQingqing】
Software environment
Microsoft Visual Studio 2010
edition 10.0.40219.1 SP1Rel
Simplify the problem
Add the following code to the project :
#include <stdio.h>
#define NameLength 10
int main()
{
char temp;
char name[NameLength];
printf(" Input name:");
scanf("%d", name);
printf(" Input temp:");
scanf("%c", &temp);
printf("name=%s\n", name);
printf("temp=%c\n", temp);
return 0;
}
The operation results are as follows : Input name when , type “ Li ”, And then line break 
Debug View related variable values :
adopt Baidu ASCII code You know :temp The value of is a newline
analysis
scanf and printf Functions read data from the data stream , For the spacer after the typed data ( In this case, the newline character ),scanf Function will put it back into the input stream , namely : The next time scanf When the function reads input , The first thing to read is , Last read was scanf The function discards the spacer .
This can explain Debug when ,temp The value of is the newline character
Next, expand the situation in the problem , If it is , The running results are different :
- The situation in the question is to use
scanfFunction to type a string , After usescanfFunction to type a single character : The operation result is abnormal , The value of the character is the spacer . - Use twice
scanfFunction to type a single character : The operation result is abnormal , The value of the second character is the spacer . - Use twice
scanfFunction to type a string : Operation result is normal
Suggest
It can be seen from the analysis that , In different cases , The spacer has different effects on the running results . therefore , As the first step to solve the problem , The compiler should be used flexibly Debug function , By looking at the values of related variables , So as to find out whether the spacer has an impact on the final running result .
If Debug after , It is found that the spacer has an impact on the final running result , Then go to the second step to solve the problem .
At present, there is no code similar to formula template , I tried a lot demo, I also learned the method in the reference link , There are many strange ways to find specific implementation methods , But the core of implementation is to solve scanf Function discards the spacer .
therefore , Recommendations are as follows :
- Try not to type characters 、 The statements that type strings are adjacent , Otherwise, it is easy to have problems ;
- Type the string with
getsfunction , Type a single character withgetcharfunction ; - You can study a piece of code logic by yourself , Take countermeasures against the spacer .
Reference link
scanf About %c The problem of
scanf Function use %s Precautions for parameters
scanf How exactly does the function read numbers ?
边栏推荐
- VIM command mode and input mode switching
- 108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
- Flet教程之 15 GridView 基础入门(教程含源码)
- Solve the problem that vscode can only open two tabs
- Software design - "high cohesion and low coupling"
- 《通信软件开发与应用》课程结业报告
- 【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
- 5V串口接3.3V单片机串口怎么搞?
- R语言可视化分面图、假设检验、多变量分组t检验、可视化多变量分组分面箱图(faceting boxplot)并添加显著性水平、添加抖动数据点(jitter points)
- 【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
猜你喜欢

Improve application security through nonce field of play integrity API

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

千人規模互聯網公司研發效能成功之路
![111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]](/img/2e/da45198bb6fb73749809ba0c4c1fc5.png)
111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]

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

【最短路】ACwing 1127. 香甜的黄油(堆优化的dijsktra或spfa)

Talk about SOC startup (IX) adding a new board to uboot

Talk about SOC startup (x) kernel startup pilot knowledge

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

Tsinghua Yaoban programmers, online marriage was scolded?
随机推荐
如何理解服装产业链及供应链
通过环境变量将 Pod 信息呈现给容器
MATLAB實現Huffman編碼譯碼含GUI界面
.NET MAUI 性能提升
Excel公式知多少?
Detailed explanation of debezium architecture of debezium synchronization
Rationaldmis2022 array workpiece measurement
What is high cohesion and low coupling?
2022年在启牛开华泰的账户安全吗?
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
[filter tracking] comparison between EKF and UKF based on MATLAB extended Kalman filter [including Matlab source code 1933]
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
How much do you know about excel formula?
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
5V串口接3.3V单片机串口怎么搞?
Present pod information to the container through environment variables
深度学习秋招面试题集锦(一)
超标量处理器设计 姚永斌 第8章 指令发射 摘录
NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
正在运行的Kubernetes集群想要调整Pod的网段地址