GETC VS的getchar VS scanf函数从标准输入读取字符 [英] getc Vs getchar Vs Scanf for reading a character from stdin
问题描述
以下三个函数:
GETC
getchar函数和放大器;
scanf函数
getc getchar & scanf
这是最好的一个用于从标准输入读取一个字符,为什么?
which is the best one for reading a character from stdin and why?
是否有任何已知的缺点或限制任何这些功能,这使得其中一个比另一个更好呢?
Are there any known disadvantages or limitations for any of these functions which makes one better than the other?
推荐答案
如果您只是想读取从标准输入一个字符,那么的getchar()
是合适的选择。如果你有更复杂的要求,那么的getchar()
将是不够的。
If you simply want to read a single character from stdin, then getchar()
is the appropriate choice. If you have more complicated requirements, then getchar()
won't be sufficient.
-
GETC()
允许你从不同的流中读取(例如,使用的fopen打开一()
) -
scanf()的
允许您一次读取不仅仅是一个单一的性格比较。
getc()
allows you to read from a different stream (say, one opened withfopen()
);scanf()
allows you to read more than just a single character at a time.
最常见的错误。您需要使用 INT
变量,因为值范围的getchar()
的回报是范围内的值的无符号的字符
,加上单一的负值 EOF
。 A 字符
变量没有足够的范围,这一点,这可能意味着你可以混淆 EOF $ C $一个完全有效的字符回报C>。这同样适用于
GETC()
。
The most common error when using getchar()
is to try and use a char
variable to store the result. You need to use an int
variable, since the range of values getchar()
returns is "a value in the range of unsigned char
, plus the single negative value EOF
". A char
variable doesn't have sufficient range for this, which can mean that you can confuse a completely valid character return with EOF
. The same applies to getc()
.
这篇关于GETC VS的getchar VS scanf函数从标准输入读取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!