getch()与ungetch()函数

1.getch()这个函数可以让用户按下任意键而不需要回车就可以接受到用户的输入。可以用来作为“press any key to continue”的实现

int getch(void)

{
    return (bufp>0) ? buf(--bufp) : getchar();

}

下面是示例代码

#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
printf("Input a character:");
ch = getch();
printf("\nYou input a '%c'\n", ch);
return 0;
}

2.ungetch()  功 能: 把一个字符退回到键盘缓冲区中

#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main( void )
{
int i=0;
char ch;
puts("Input an integer followed by a char:");
/* read chars until non digit or EOF */
while((ch = getche()) != EOF && isdigit(ch))
i = 10 * i + ch - 48; /* convert ASCII into int value */
/* if non digit char was read, push it back into input buffer */
if (ch != EOF)
ungetch(ch);
printf("\n\ni = %d, next char in buffer = %c\n", i, getch());
return 0;
}


void ungetch(int c)
{
    if(bufp>=BUFSIZE)
       printf("ungetch:too many charactors\n");
    else
       buf[bufp++]=c;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值