当前位置:网站首页>C language: getchar () and cache
C language: getchar () and cache
2022-07-29 04:00:00 【Xiaowa 123】
When we use the keyboard to type in character information , In fact, the typed characters are stored in the cache , When pressing a series of characters on the screen ( Formed a string ) With the Enter key , There will also be characters and enter keys in the cache (\n) Appear as a character . and getchar The function reads characters one by one from the cache .
For example, press on the screen abc Back carriage return , What appears in the cache is abc\n.
Every time the program is called getchar() function ,getchar Just read a character from the cache and store it in the corresponding variable . Again ,\n It will also be used as a character getchar Read .
about putchar() In terms of functions , The function is to print character variables on the screen , If putchar() The parameter of is just \n when , Then there will be no display on the screen , Carriage return character \n Also will be getchar Read a character .
in general :getchar Read the characters one by one , But the input characters are stored in the cache first , When enter is pressed getchar Just start reading data in the cache , Every getchar Functions only read one character .putchar Display characters , If you give putchar Yes. \n, That's the carriage return , that putchar Will not be displayed .
Here is a program to illustrate :
#include <stdio.h>
int main()
{
int c;
while((c=getchar())!=EOF)
{
putchar(c);
c=getchar();
}
return 0;
} Run the program , Input 123, And then go back . result :13
analysis : When while At the beginning of the program , As while The judgment condition of cycle , here getchar The function starts waiting for the keyboard to enter character information , When the input 123, And then go back . When getchar Function determines that enter has been pressed , That is, the buffer has been flushed , Start reading data from the cache , At this time, the cache is :123\n
Get into while loop , As a circular judgment condition ,getchar Read the first character 1, accord with while The loop condition , Enter loop execution putchar, The output characters 1.
Go down ,getchar The function is called again , Because there are still 23\n, because getchar Function has not read \n, So continue to read characters in the cache 2, here c=2. Go back to while loop , As a condition of judgment ,getchar Continue reading cache characters , here c=3, Into the loop ,putchar Print character 3, Keep going down ,getchar Read \n. Go back to the cycle condition ,getchar It is found that there are no characters in the cache to read , here getchar()=-1, That is, it no longer meets the cycle condition , Now jump out of the loop , So the end result is 13.
in general ,getchar When will a value of EOF(-1) In the case? , That is when getchar Function finds that there are no characters in the buffer to read , Even if it's \n It has also been called before getchar Read go , That is, when the buffer is empty ,getchar The return value of will be -1.
边栏推荐
- Data mining -- Introduction to the basis of association analysis (Part 1)
- [introduction to C language] zzulioj 1031-1035
- 力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
- 1. Mx6u driver development-2-led driver
- Several cases of word wrapping in div
- 【C语言入门】ZZULIOJ 1031-1035
- The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
- 企业网的三层架构
- Typescript from getting started to mastering (19) enumeration types
- Cannot paste multiple pictures at once
猜你喜欢

面试必备!TCP协议经典十五连问!

Android view system and custom view Series 1: (kotlin version)

Solve the problem of garbled code when opening the project code in idea

CUB_ Visualization of key points in 200 bird dataset

@Configuration (proxybeanmethods = false) what's the use of setting this to false

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

小马智行进军前装量产,从自研域控制器入手?

向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输

1985-2020 (8 Editions) global surface coverage download and introduction

RHCE's at, crontab's basic operations, the Chrony service, symmetric encryption and asymmetric encryption
随机推荐
LDP --- 标签分发协议
5年多工作经验,工资给15k,要是你,你会接受吗?
Solve the delay in opening the console of Google browser
"The programming is not standardized, and my colleagues are in tears!"
《陌路曾相逢》夏陌沈疏晏_夏陌沈疏晏最新章节
LDP -- label distribution protocol
sql
【redis系列】字符串数据结构
Tristate gate
OA项目之会议通知(查询&是否参会&反馈详情)
Why don't programmers work blindly?
Data mining -- Introduction to the basis of association analysis (Part 1)
Why do programmers so "dislike" the trunk development mode?
Vs code must know and know 20 shortcut keys!
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
Data mining -- code implementation of association analysis example (Part 2)
Flask framework operation database_ Add, delete, modify and query statements
C language to achieve three chess game (detailed explanation)
Simple cases of inner connection and left connection
向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用