头文件:#include<stdio.h>
ungetch()函数用于将一个字符回退到键盘缓冲区,其原型为:
int ungetch( int ch );
【参数】ch为要退回的字符。
【返回值】若成功,则返回该字符,否则返回EOF。
- #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;
- }
运行结果:
Input an integer followed by a char:
k↙
i = 0, next char in buffer = k
请按任意键继续. . .