getc()、getchar()、getch() 和 getche() 的区别 | 您所在的位置:网站首页 › getchar在c语言中的用法 › getc()、getchar()、getch() 和 getche() 的区别 |
所有这些函数都从输入中读取一个字符并返回一个整数值。返回整数以容纳用于指示失败的特殊值。EOF值通常用于此目的。 getc()它从给定的输入流中读取单个字符,并在成功时返回相应的整数值(通常是读取字符的ASCII值)。失败时返回EOF。 语法: int getc(FILE *stream);示例: // Example for getc() in C #include 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 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 定义的。 与上述函数一样,它也从键盘读取单个字符。 但它不使用任何缓冲区,因此输入的字符会立即返回,无需等待回车键。 语法: int getch();示例: // Example for getch() in C #include #include 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 #include // 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' 参考文档[1]Vankayala Karunakar.Difference between getc(), getchar(), getch() and getche()[EB/OL].https://www.geeksforgeeks.org/difference-getchar-getch-getc-getche/,2018-12-13. |
CopyRight 2018-2019 实验室设备网 版权所有 |