当前位置:网站首页>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.
边栏推荐
- 2. Variables and scope
- Cloudera manager platform fault repair record
- Why is continuous integration and deployment important in development?
- Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
- tron OUT_ OF_ ENERGY
- How fast does it take to implement a super simple language
- 3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
- 3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘
- 企业网的三层架构
- Arrow function of new features of ES6
猜你喜欢

企业网的三层架构

Object array merges elements according to a field

RHCE的at,crontab的基本操作,chrony服务和对称加密和非对称加密

Malloc C language

CUB_ Visualization of key points in 200 bird dataset

CUB_200鸟类数据集关键点可视化

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

Batch production and upload sales NFT opensea eth polygon

Configmap配置与Secret加密

Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
随机推荐
1985-2020 (8 Editions) global surface coverage download and introduction
What have I learned from 200 machine learning tools?
座机的表单验证
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
MySQL Part 3
当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
代码 ~ 隐藏或禁用状态栏和虚拟按键
企业网的三层架构
Microcomputer principle and interface technology
SFTP upload error: com.jcraft.jsch JSchException: connection is closed by foreign host
Remote desktop connection error
Overestimated test driven development?
Malloc C language
华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...
Analysis of new retail o2o e-commerce model
从2019 年开始,你一定停止使用了这个营销策略…
UCOS task switching process
2. Variables and scope
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
[principle] several ways of horizontal penetration