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

所有这些函数都从输入中读取一个字符并返回一个整数值。返回整数以容纳用于指示失败的特殊值。EOF值通常用于此目的。

null

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 

示例应用程序: C程序比较两个文件并报告不匹配

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头文件,主要由Turbo C等MS-DOS编译器使用。它不是C标准库或ISO C的一部分,也不是由POSIX定义的(来源:http://en.wikipedia.org/wiki/Conio.h) 与上述功能一样,它还可以从键盘读取单个字符。但它不使用任何缓冲区,因此输入的字符会立即返回,而无需等待enter键。 语法:

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'

本文由 瓦卡亚拉·卡鲁纳卡 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享