printf 未在控制台上打印 [英] printf not printing on console

查看:52
本文介绍了printf 未在控制台上打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 C 语言.我使用 eclipse (juno) 作为我的 IDE 并安装了 CDT 插件.我还解压了 mingw64(GCC 编译器).我写了一个非常简单的程序来看看它是否有效.这是我的代码:

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:

#include <stdio.h>

int main()
{
    int age;
    printf("Hello, please enter your age:
");
    scanf("%d", &age);
    printf("Your age is %d", age);
    return 0;
}

问题是输出缓冲区填充了第一个printf的字符串值,但没有输出到控制台.我必须输入一个数字,然后缓冲区才将所有数据倒入控制台,所以我看到控制台是这样的:

The problem is that the output buffer is filled with the string value of the first printf but 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:

1
Hello, please enter your age:
Your age is 1

而不是预期的是:

Hello, please enter your age:
1
Your age is 1

现在,我发现我可以在第一个 printf 之后使用 fflush(stdout) 但我不认为这个解决方案是优雅的,甚至是必要的.关于如何克服这个问题的任何想法?

Now, I found that I can use fflush(stdout) after the first printf but I don't think that this solution is elegant and even necessary. Any ideas on how I can overcome this?

编辑 - 因为我是在大学里学的,所以我不能使用课程中没有学到的任何东西,所以我可以使用 printfscanf

EDIT - because I'm learning this in my university, I can't use anything that wasn't learned in the course so I can only use printf and scanf

新编辑 - 我想我已经找到了对此的解释.正如我所说,我正在输出到 Eclipse 中的控制台视图.奇怪的是,如果我从 Windows 的命令行编译并运行程序,我会得到想要的结果.因此,我认为 eclipse 实际上是将输出写入文件并在控制台窗口中呈现.如何强制 Eclipse 在我的运行配置中打开一个真正的命令行窗口?

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 的一个已知错误.此错误已通过 WONT-FIX 的分辨率解决.我不知道为什么.链接在这里:Eclipse C 控制台错误.

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.

这篇关于printf 未在控制台上打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆