C语言 printf 未在控制台上打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13035075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
printf not printing on console
提问by Mr T.
I'm getting started in the C language. I am using eclipse (juno) as my IDE and installed CDT plugin. I have also unpacked mingw64 (GCC Compiler). I wrote a very simple program to see if it works. This is my code:
我开始使用 C 语言。我使用 eclipse (juno) 作为我的 IDE 并安装了 CDT 插件。我还解压了 mingw64(GCC 编译器)。我写了一个非常简单的程序来看看它是否有效。这是我的代码:
#include <stdio.h>
int main()
{
int age;
printf("Hello, please enter your age:\n");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
The problem is that the output buffer is filled with the string value of the first printfbut does not output it to the console. I have to enter a number, and only then the buffer pours all the data to the console so I see the console something like this:
问题是输出缓冲区填充了第一个的字符串值,printf但没有将其输出到控制台。我必须输入一个数字,然后缓冲区才将所有数据倒入控制台,所以我看到控制台是这样的:
1
Hello, please enter your age:
Your age is 1
instead of what is expected that is:
而不是预期的是:
Hello, please enter your age:
1
Your age is 1
Now, I found that I can use fflush(stdout)after the first printfbut I don't think that this solution is elegant and even necessary. Any ideas on how I can overcome this?
现在,我发现我可以fflush(stdout)在第一个之后使用,printf但我不认为这个解决方案是优雅的,甚至是必要的。关于如何克服这个问题的任何想法?
EDIT- because I'm learning this in my university, I can't use anything that wasn't learned in the course so I can onlyuse printfand scanf
编辑- 因为我在我的大学学习这个,我不能使用课程中没有学到的任何东西,所以我只能使用printf和scanf
NEW EDIT- I think I have found an explanation for this. As I said, I am outputting to the console view inside Eclipse. The strange thing is that if I compile and run the program from the command line of Windows, I get the wanted result. Therefore, I think that eclipse is actually writing the output to a file and presenting it in the console window. How can I force eclipse to open a real command line window in my run configurations?
新编辑- 我想我已经找到了对此的解释。正如我所说,我正在输出到 Eclipse 中的控制台视图。奇怪的是,如果我从 Windows 的命令行编译并运行程序,我会得到想要的结果。因此,我认为 eclipse 实际上是将输出写入文件并在控制台窗口中呈现。如何强制 eclipse 在我的运行配置中打开一个真正的命令行窗口?
采纳答案by Mr T.
Apparently this is a known bug of Eclipse. This bug is resolved with the resolution of WONT-FIX. I have no idea why though. here is the link: Eclipse C Console Bug.
显然,这是 Eclipse 的一个已知错误。此错误已通过 WONT-FIX 的分辨率解决。我不知道为什么。这是链接: Eclipse C Console Bug。
回答by kliteyn
Output is buffered.
输出被缓冲。
stdout is line-buffered by default, which means that '\n' is supposed to flush the buffer. Why is it not happening in your case? Dunno. Need more info about your application/environment.
默认情况下,标准输出是行缓冲的,这意味着 '\n' 应该刷新缓冲区。为什么在你的情况下没有发生?不知道。需要有关您的应用程序/环境的更多信息。
However, you can control buffering with setvbuf():
但是,您可以使用 setvbuf() 控制缓冲:
setvbuf(stdout, NULL, _IOLBF, 0);
This will force stdout to be line-buffered.
这将强制标准输出被行缓冲。
setvbuf(stdout, NULL, _IONBF, 0);
This will force stdout to be unbuffered, so you won't need to use fflush(). Note that it will severely affect application performance if you have lots of prints.
这将强制标准输出无缓冲,因此您不需要使用 fflush()。请注意,如果您有大量打印件,它将严重影响应用程序性能。
回答by user1500049
Try setting this before you print:
在打印之前尝试设置:
setvbuf (stdout, NULL, _IONBF, 0);
回答by Kitchi
回答by Tyler
As others have pointed out, output can be buffered within your program before a console or shell has a chance to see it.
正如其他人指出的那样,输出可以在控制台或外壳程序有机会看到它之前缓冲在您的程序中。
On unix-like systems, including macs, stdouthas line-based buffering by default. This means that your program empties its stdoutbuffer as soon as it sees a newline.
在类 Unix 系统(包括 macs)上,stdout默认情况下具有基于行的缓冲。这意味着您的程序在stdout看到换行符后立即清空其缓冲区。
However, on windows, newlines are no longer special, and full buffering is used. Windows doesn't support line buffering at all; see the msdn page on setvbuf.
但是,在 Windows 上,换行不再是特殊的,而是使用了全缓冲。Windows 根本不支持行缓冲;请参阅setvbuf 上的 msdn 页面。
So on windows, a good approach is to completely shut off stdoutbuffering like so:
因此,在 Windows 上,一个好的方法是完全关闭stdout缓冲,如下所示:
setvbuf (stdout, NULL, _IONBF, 0);
回答by babyinEclipse
- In your project folder, create a “.gdbinit” text file. It will contain your gdb debugger configuration
- Edit “.gdbinit”, and add the line (without the quotes) : “set new-console on”
After building the project right click on the project Debug > “Debug Configurations”, as shown below

In the “debugger” tab, ensure the “GDB command file” now points to your “.gdbinit” file. Else, input the path to your “.gdbinit” configuration file :

Click “Apply” and “Debug”. A native DOS command line should be launched as shown below

- 在您的项目文件夹中,创建一个“.gdbinit”文本文件。它将包含您的 gdb 调试器配置
- 编辑“.gdbinit”,并添加一行(不带引号):“set new-console on”
建好工程后右键工程Debug > “Debug Configurations”,如下图

在“调试器”选项卡中,确保“GDB 命令文件”现在指向您的“.gdbinit”文件。否则,输入“.gdbinit”配置文件的路径:

单击“应用”和“调试”。应启动本机 DOS 命令行,如下所示

回答by user2838962
Add c:\gygwin\binto PATHenvironment variable either as a system environment variable or in your eclipse project (properties-> run/debug-> edit)
添加c:\gygwin\bin到PATH环境变量作为系统环境变量或在您的 eclipse 项目中(属性-> 运行/调试-> 编辑)

