printf()在Eclipse中的不明确行为
我是缓冲流的新手。我正在编写一个简单的c程序,它将字符串作为用户输入并显示出来。我的工作环境是windows下的eclipse。代码如下:printf()在Eclipse中的不明确行为,c,eclipse,stream,C,Eclipse,Stream,我是缓冲流的新手。我正在编写一个简单的c程序,它将字符串作为用户输入并显示出来。我的工作环境是windows下的eclipse。代码如下: #include<stdio.h> enum { max_string = 127 }; static char string[max_string+1] = ""; void main(){ printf("type the input string---> \n"); fgets(string,max_strin
#include<stdio.h>
enum { max_string = 127 };
static char string[max_string+1] = "";
void main(){
printf("type the input string---> \n");
fgets(string,max_string,stdin);
printf("the input string was ---> %s\n",string);
}
type the input string--->
user input
the input string was ---> user input
我在CodeBlocks IDE中尝试了上面的代码,效果很好。输出如下:
#include<stdio.h>
enum { max_string = 127 };
static char string[max_string+1] = "";
void main(){
printf("type the input string---> \n");
fgets(string,max_string,stdin);
printf("the input string was ---> %s\n",string);
}
type the input string--->
user input
the input string was ---> user input
有什么问题?
我还在我的printf()
的最后一页添加了一个\n
,以便立即刷新它们
问候。好的!!通过谷歌搜索,我发现eclipse终端仿真器比普通终端做更多的缓冲在
printf
之后进行code>以使其工作。stdout
仅在连接到终端时才进行线路缓冲。Eclipse的终端模拟可能未被检测为终端
发件人:
当流stdout指向一个终端时,它被行缓冲
有几种方法可以绕过此限制:
- 每次出现输出时调用
fflush(stdout)
- 通过这样使用将
设置为无缓冲stdout
在使用标准输出之前setvbuf(stdout, NULL, _IONBF, 0);
- 使用默认情况下不缓冲的
stderr
fflush(stdout)代码>在printf
@Prakash之后,谢谢。我想知道,是否有任何永久性的解决方案。我的意思是,有没有办法禁用这个额外的缓冲。您使用哪个版本的Eclipse来观察这种行为。还有它运行在哪个操作系统上。我无法在Debian Stable上的Eclipse Juno SR1上复制这一点。