问题描述
因此,快速Google搜索 fflush(stdin)
清除输入缓冲区会显示大量网站警告使用它。
如何使用 fflush(stdin)
?
简单:这是一个很简单的方法:是未定义的行为,因为 fflush
意味着在输出流上调用。这是C标准的摘录:
所以这不是一个多糟的问题。 fflush(stdin)
是明显错误,而且不得使用。
So a quick Google search for fflush(stdin)
for clearing the input buffer reveals numerous websites warning against using it. And yet that's exactly how my CS professor taught the class to do it.
How bad is using fflush(stdin)
? Should I really abstain from using it, even though my professor is using it and it seems to work flawlessly?
Simple: this is undefined behavior, since fflush
is meant to be called on an output stream. This is an excerpt from the C standard:
So it's not a question of "how bad" this is. fflush(stdin)
is plainly wrong, and you mustn't use it, ever.
这篇关于使用fflush(stdin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!