fflush(stdin)函数在linux下无效

今天在编程的时候发现一个有关fflush刷新缓冲区问题:

#include<stdio.h>
int main(int argc, char const *argv[])
{
	char c;
	int number;
	scanf("%d",&number);
	fflush(stdout);
	c = getchar();
	putchar(c);
	return 0;
}

在我输入一个数字之后再想要接收一个字符的时候,它会直接跳过getchar函数。我当时一看知道是getchar接收了上一个输入中的回车字符了,于是我就想到用fflush文件刷新函数,本以为可以轻松解决问题,但是。。。
shortcut

并没有什么用。。。
于是我换了getchar来接受字符就好了,不过这就引起我的疑惑了。
于是在网上查了以下资料发现:
在windows VC下fflush(stdin)是可以实现的,但是linux下不可以。 C标准规定fflush()函数是用来刷新输出(stdout)缓存的。对于输入(stdin),它是没有定义的。但是有些编译器也定义了 fflush(stdin )的实现,比如微软的VC。其它编译器是否也定义了 fflush( stdin)的实现应当查找它的手册。GCC编译器没有定义它的实现,所以不能使用 fflush( stdin )来刷新输入缓存。

The ANSI/ISO standard
specifies that fflush() is useful only on OUTPUT streams.

猜你喜欢

转载自blog.csdn.net/qq_37414405/article/details/83410587