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

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

问题描述

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

  #include< stdio.h> 

int main()
{
int age;
printf(你好,请输入你的年龄:\\\
);
scanf(%d,& age);
printf(你的年龄是%d,年龄);
return 0;
}

问题是输出缓冲区用第一个 printf ,但不会将其输出到控制台。我必须输入一个数字,只有缓冲区将所有的数据输入到控制台,所以我看到控制台这样的:

  1 
您好,请输入您的年龄:
您的年龄为1

而不是预期的是:

 您好,请输入您的年龄:
1
您的年龄是1

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



编辑 - 因为我正在我的大学学习,我不能使用课程中没有学到的东西,所以我可以 使用 printf scanf



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

解决方案

OK - 显然这是一个已知的错误的日食该错误通过WONT-FIX的解决方案得以解决。
不知道为什么。这里是链接:

Eclipse C控制台Bug < a>


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:\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 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

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 printf but I don't think that this solution is elegant and even necessary. Any ideas on how I can overcome this?

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

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?

解决方案

OK - apparently this is a known bug of eclipse. This bug is resolved with the resolution of WONT-FIX. have no idea why though. here is the link:
Eclipse C Console Bug

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

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