当前位置:网站首页>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
scanf
Function to type a string , After usescanf
Function to type a single character : The operation result is abnormal , The value of the character is the spacer . - Use twice
scanf
Function to type a single character : The operation result is abnormal , The value of the second character is the spacer . - Use twice
scanf
Function 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
gets
function , Type a single character withgetchar
function ; - 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 ?
边栏推荐
- STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
- The Oracle message permission under the local Navicat connection liunx is insufficient
- 人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
- 18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
- 让数字管理好库存
- Talk about SOC startup (x) kernel startup pilot knowledge
- How to connect 5V serial port to 3.3V MCU serial port?
- Swiftui tutorial how to realize automatic scrolling function in 2 seconds
- 111.网络安全渗透测试—[权限提升篇9]—[Windows 2008 R2内核溢出提权]
- R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
猜你喜欢
CMU15445 (Fall 2019) 之 Project#2 - Hash Table 详解
总结了200道经典的机器学习面试题(附参考答案)
一度辍学的数学差生,获得今年菲尔兹奖
Reasons for the failure of web side automation test
Common locking table processing methods in Oracle
Onedns helps college industry network security
【神经网络】卷积神经网络CNN【含Matlab源码 1932期】
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
[neural network] convolutional neural network CNN [including Matlab source code 1932]
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
随机推荐
MATLAB實現Huffman編碼譯碼含GUI界面
正在运行的Kubernetes集群想要调整Pod的网段地址
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
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?
What development models did you know during the interview? Just read this one
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
In SQL, I want to set foreign keys. Why is this problem
[Yugong series] go teaching course 005 variables in July 2022
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
超标量处理器设计 姚永斌 第8章 指令发射 摘录
[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]
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
EasyUI learn to organize notes
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
Easyui学习整理笔记
STM32 entry development uses IIC hardware timing to read and write AT24C08 (EEPROM)