c语言scanf - fflush(stdin):无效[重复]

时间:2021-05-28 07:29:39

This question already has an answer here:

这个问题已经有了答案:

When I use scanf more than one time the program do not wait for another input. Instead it exits

当我多次使用scanf时,程序不会等待另一个输入。相反,它退出

I learned that I could put a blank space before the conversion specifier in the scanf-function - yes that solved the problem and I guess that has to do with the inputstream, that is - if its a newline character in the inputstream the scanf will consume it immediately.

我了解到,我可以在scanf函数的转换说明符之前放置一个空格——是的,这解决了问题,我猜这与inputstream有关,也就是说——如果它是inputstream中的换行字符,scanf将立即使用它。

scanf(" %f", &value);

But if its so - why could I not use the fflush(stdin) instead? I have tried but it doesnt work.

但是如果是这样的话——为什么我不能用fflush(stdin)代替呢?我试过了,但没用。

#include <stdio.h>

int main(void)
{
    float value;
    char ch;

    printf("input value: ");
    scanf("%f", &value);
    fflush(stdin);
    printf("input char: ");
    scanf("%c", &ch);

    return 0;
}

2 个解决方案

#1


8  

fflush() is used for clearing output buffers. Since you are trying to clear an input buffer, this may lead to undefined behavior.

fflush()用于清除输出缓冲区。由于您试图清除一个输入缓冲区,这可能导致未定义的行为。

Here is an SO question explaining why this isn't good practice :

这里有一个问题可以解释为什么这不是一个好的实践:

Using fflush(stdin)

使用。fflush(stdin)

#2


5  

AS per the C11 standard document, chapter 7.21.5.2, fflush() function, (emphasis mine)

根据C11标准文档,第7.21.5.2章,fflush()函数,(重点是我的)

int fflush(FILE *stream);

int。fflush(文件*流);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

如果流指向一个输出流或一个更新流,其中最近的操作没有被输入,则fflush函数会导致将该流传递到主机环境的任何未写入数据写入文件;否则,行为就没有定义。

so, basically, using fflush(stdin); is undefined behaviour.

所以,基本上,使用。fflush(stdin);是未定义的行为。

To serve your purpose, while using %c format specifier, you can rewrite your code as

为了达到您的目的,在使用%c格式说明符时,可以将代码重写为

scanf(" %c", &ch);
       ^
       |
   notice here

the leading whitespace before %c skips all whitespace like character (including the \n stored by pressing previous ENTER key) and read the first non-whitespace character.

在%c之前的前导空格跳过所有的空格,如字符(包括按前一个ENTER键存储的\n),并读取第一个非空格字符。

Note: as %d and %f specifiers already internally ignore the leading whitespaces, you don't need to speicy explicitly in those cases.

注意:由于%d和%f说明符已经在内部忽略了主要的白空间,所以在这些情况下不需要显式地进行吐出。

#1


8  

fflush() is used for clearing output buffers. Since you are trying to clear an input buffer, this may lead to undefined behavior.

fflush()用于清除输出缓冲区。由于您试图清除一个输入缓冲区,这可能导致未定义的行为。

Here is an SO question explaining why this isn't good practice :

这里有一个问题可以解释为什么这不是一个好的实践:

Using fflush(stdin)

使用。fflush(stdin)

#2


5  

AS per the C11 standard document, chapter 7.21.5.2, fflush() function, (emphasis mine)

根据C11标准文档,第7.21.5.2章,fflush()函数,(重点是我的)

int fflush(FILE *stream);

int。fflush(文件*流);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

如果流指向一个输出流或一个更新流,其中最近的操作没有被输入,则fflush函数会导致将该流传递到主机环境的任何未写入数据写入文件;否则,行为就没有定义。

so, basically, using fflush(stdin); is undefined behaviour.

所以,基本上,使用。fflush(stdin);是未定义的行为。

To serve your purpose, while using %c format specifier, you can rewrite your code as

为了达到您的目的,在使用%c格式说明符时,可以将代码重写为

scanf(" %c", &ch);
       ^
       |
   notice here

the leading whitespace before %c skips all whitespace like character (including the \n stored by pressing previous ENTER key) and read the first non-whitespace character.

在%c之前的前导空格跳过所有的空格,如字符(包括按前一个ENTER键存储的\n),并读取第一个非空格字符。

Note: as %d and %f specifiers already internally ignore the leading whitespaces, you don't need to speicy explicitly in those cases.

注意:由于%d和%f说明符已经在内部忽略了主要的白空间,所以在这些情况下不需要显式地进行吐出。