当前位置:网站首页>Clear the buffer with getchar (strongly recommended, C language is error prone, typical)
Clear the buffer with getchar (strongly recommended, C language is error prone, typical)
2022-07-23 11:19:00 【rookieﻬ°】
How to clean the buffer ??
What is a buffer ?
Buffer is also called cache , Is a part of memory , Is the storage space reserved in memory , Used to buffer input or output data . According to its corresponding output device or output device , Divided into input buffer and output buffer .
Why clean up the buffer ?
When we read information on disk , The read data will be put into the buffer first , The computer then reads data from the buffer , When the data in the buffer is read , The computer will repeat the above operations as required , Until the end of user reading . however , When we don't need the contents of the buffer and want to skip it , When reading the next part directly , Can we read it directly ? Obviously not ! At this time, we need to clear the buffer ;
How to clean the buffer ?
example : Judge whether it can operate normally ?
#include<stido.h>
int main()
{
char password[10] = {
0};
printf(" Please input a password :\n");
scanf("%s",password);// The array name is the address , Remember this
printf(" Please confirm the password (Y/N):");
int ch = gerchar();//getchar The return type of is int type , If it is set to char type , Possible data loss
if(ch == 'Y')
{
printf(" Confirm success \n");
}
else
{
printf(" Confirmation failed \n");
}
return 0;
}
The answer is : You can't .
Why? ?[^1]
[1^]: The picture is taken from xiaoxianying emojil.
Because when you are scanf After entering the password , Your return is also put in the buffer , however scanf Read the contents before entering , But left the carriage return symbol in the buffer , This causes when you getchar() When , There is something in the buffer , thus getchar() Directly read the carriage return .
resolvent :
step :
getchar();
In this way, a character in the buffer can be cleared , For example, the last question ;
What if I want to clean up a lot of garbage in the buffer zone ? This time we need to use the cycle .
while(getchar()!='\0');
This cycle is very short and delicate , You analyze it carefully , Will find , Finally, the carriage return is cleared !
I think , friend , You don't want others to prostitute your article for nothing 【 Manual funny 】.
边栏推荐
猜你喜欢

Fun code rain, share it online~-

TypeScript介绍

Redis数据库和项目框架

【无标题】
D2DEngine食用教程(1)———最简单的程序

my_strcpy的实现(经典,简单,实用,收藏)

JDBC Learning and simple Encapsulation

Copy a project /project in idea

【C语言】什么是函数?函数的分类和侧重(帮你快速分类和记忆函数)

How to merge the website content with video and audio separated? How to write batch download code?
随机推荐
[python flask notes 5] blueprint is easy to use
Install pyGame using CMD
第一篇博客
【Pyautogui学习】屏幕坐标、鼠标滚动
Spark common interview questions sorting
js的防抖和节流
[监控部署实操]基于granfana展示Prometheus的图表和loki+promtail的图表
视图集及路由
【Pyradiomics】提取的影像组学特征值不正常(很多0和1)
机器学习中的矩阵向量求导
When using cache in sprintboot, the data cannot be loaded
[Doris]配置和基本使用contens系统(有时间继续补充内容)
JWT header进行编码过程
systemctl-service服务添加环境变量及模板
项目流程总结
Summary of common commands of vim
Using pytorch to realize the flower recognition classifier based on VGg 19 pre training model, the accuracy reaches 97%
Use of views
Framework introduction Mvt
[Hudi]hudi的编译及hudi&spark和hudi&flink的简单使用