当前位置:网站首页>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.
边栏推荐
- Is the browser multi process or single process?
- Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
- 【C语言入门】ZZULIOJ 1031-1035
- HCIP BGP
- Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
- Uni app internationalization
- Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
- 小马智行进军前装量产,从自研域控制器入手?
- 第一个ALV程序2
- 1. Mx6u driver development-2-led driver
猜你喜欢

数据挖掘——关联分析基础介绍(上)

Typescript from getting started to mastering (XX) function generics

【深度学习CPU(番外篇)——虚拟内存】

Vs code must know and know 20 shortcut keys!

Is the browser multi process or single process?

Configmap配置与Secret加密

What you see and think in Microsoft

LDP -- label distribution protocol

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

Raft protocol - process demonstration
随机推荐
Connect with third-party QQ login
路由 知识
1. Header file - Comment - namespace - standard input / output stream
Configmap配置与Secret加密
初识C语言(3)
Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
代码 ~ 隐藏或禁用状态栏和虚拟按键
Typescript from introduction to proficiency (XXIV) using import syntax
The const keyword of ES6 declares variables
Casbin入门
Configmap configuration and secret encryption
About the writing of ALV format control part
Tristate gate
After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
MySQL Part 3
SFTP upload error: com.jcraft.jsch JSchException: connection is closed by foreign host
MySQL Part 4 (end)
向日葵资深产品总监技术分享:“国民远控”如何在AD域环境下应用
路西法98-生活记录ing
5年多工作经验,工资给15k,要是你,你会接受吗?