当前位置:网站首页>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 ?
边栏推荐
- 108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
- 【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
- Complete collection of common error handling in MySQL installation
- 【最短路】Acwing1128信使:floyd最短路
- UP Meta—Web3.0世界创新型元宇宙金融协议
- R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
- Fleet tutorial 19 introduction to verticaldivider separator component Foundation (tutorial includes source code)
- Rationaldmis2022 array workpiece measurement
- When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
- EasyUI learn to organize notes
猜你喜欢
![[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]

UP Meta—Web3.0世界创新型元宇宙金融协议

Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
![[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]](/img/65/bf1d0f82878a49041e8c2b3a84bc15.png)
[extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]

软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
![112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]](/img/b6/6dfe9be842204567096d1f4292e8e7.png)
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
![[system design] index monitoring and alarm system](/img/8e/9c4c168f7f2b8e1f0786a5fe158544.png)
[system design] index monitoring and alarm system

Flet教程之 17 Card卡片组件 基础入门(教程含源码)

千人規模互聯網公司研發效能成功之路

Swiftui tutorial how to realize automatic scrolling function in 2 seconds
随机推荐
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
How to write test cases for test coupons?
Use references
The running kubernetes cluster wants to adjust the network segment address of pod
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
Various uses of vim are very practical. I learned and summarized them in my work
sql里,我想设置外键,为什么出现这个问题
Talk about SOC startup (VI) uboot startup process II
C#中在路径前加@的作用
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Have you ever met flick Oracle CDC, read a table without update operation, and read it repeatedly every ten seconds
What development models did you know during the interview? Just read this one
相机标定(2): 单目相机标定总结
超标量处理器设计 姚永斌 第9章 指令执行 摘录
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
Complete collection of common error handling in MySQL installation
In SQL, I want to set foreign keys. Why is this problem
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
[filter tracking] strapdown inertial navigation simulation based on MATLAB [including Matlab source code 1935]
Improve application security through nonce field of play integrity API