当前位置:网站首页>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 ?
边栏推荐
- 从工具升级为解决方案,有赞的新站位指向新价值
- 【系统设计】指标监控和告警系统
- [system design] index monitoring and alarm system
- C#中在路径前加@的作用
- Improve application security through nonce field of play integrity API
- powershell cs-UTF-16LE编码上线
- R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
- Use references
- CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
- Technology sharing | packet capturing analysis TCP protocol
猜你喜欢

SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算

千人規模互聯網公司研發效能成功之路
![[neural network] convolutional neural network CNN [including Matlab source code 1932]](/img/65/cf9d0a3f46a581dc8f28de2e28779d.png)
[neural network] convolutional neural network CNN [including Matlab source code 1932]

Cmu15445 (fall 2019) project 2 - hash table details

Visual Studio 2019 (LocalDB)\MSSQLLocalDB SQL Server 2014 数据库版本为852无法打开,此服务器支持782版及更低版本

Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?

CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解

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

Rationaldmis2022 array workpiece measurement

.NET MAUI 性能提升
随机推荐
R语言使用quantile函数计算评分值的分位数(20%、40%、60%、80%)、使用逻辑操作符将对应的分位区间(quantile)编码为分类值生成新的字段、strsplit函数将学生的名和姓拆分
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
Talk about SOC startup (IX) adding a new board to uboot
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
Summed up 200 Classic machine learning interview questions (with reference answers)
Enclosed please find. Net Maui's latest learning resources
The Oracle message permission under the local Navicat connection liunx is insufficient
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
Rationaldmis2022 array workpiece measurement
How much do you know about excel formula?
Complete collection of common error handling in MySQL installation
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
相机标定(1): 单目相机标定及张正友标定基本原理
OneDNS助力高校行业网络安全
What is high cohesion and low coupling?
本地navicat连接liunx下的oracle报权限不足
Talk about SOC startup (VI) uboot startup process II
Basic introduction to the 16 tabs tab control in the fleet tutorial (the tutorial includes source code)
正在運行的Kubernetes集群想要調整Pod的網段地址