吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → getc()、getchar()、getch()和getche()的区别

getc()、getchar()、getch()和getche()的区别

来源:网络 | 更新时间:2022-01-08 16:17:56
所有这些函数都从输入中读取一个字符并返回一个整数值。返回整数以容纳用于指示失败的特殊值。值 EOF 通常用于此目的。 getc(): 它从给定的输入流中读取单个字符,并在成功时返回相应的整数值(通常是读取字符的 ASCII 值)。它在失败时返回 EOF。 语法:
int getc(FILE *stream); 
例子:
// Example for getc() in C
#include <stdio.h>
int main()
{
printf("%c", getc(stdin));
return(0);
}
Input: g (press enter key)
Output: g
getchar(): getc() 和getchar() 的区别在于getc() 可以从任何输入流中读取,而getchar() 是从标准输入中读取。所以 getchar() 等价于 getc(stdin)。 语法:
int getchar(void); 
例子:
// Example for getchar() in C
#include <stdio.h>
int main()
{
printf("%c", getchar());
return 0;
}
Input: g(press enter key)
Output: g
getch(): getch() 是一个非标准函数,存在于 conio.h 头文件中,该文件主要由 MS-DOS 编译器(如 Turbo C)使用。它不是 C 标准库或 ISO C 的一部分,也没有定义由 POSIX 提供(来源:http://en.wikipedia.org/wiki/Conio.h) 与上述功能一样,它也从键盘读取单个字符。但它不使用任何缓冲区,因此输入的字符立即返回,无需等待回车键。 语法:
int getch();
例子:
// Example for getch() in C
#include <stdio.h>
#include <conio.h>
int main()
{
printf("%c", getch()); 
return 0;
}
Input:  g (Without enter key)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        it shows a single g, i.e., 'g'
getche() 与 getch ()一样,这也是 conio.h 中存在的非标准函数。它从键盘读取单个字符并立即显示在输出屏幕上,而无需等待回车键。 语法:
int getche(void); 
例子:
#include <stdio.h>
#include <conio.h>
// Example for getche() in C
int main()
{
printf("%c", getche());
return 0;
}
Input: g(without enter key as it is not buffered)
Output: Program terminates immediately.
        But when you use DOS shell in Turbo C, 
        double g, i.e., 'gg'
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com