eclipse中的关于scanf和printf的输入顺序的解决办法
2010-02-28 10:47
435 查看
The eclipse console has weird behaviour when used for input with C programs.
I teach C to first year undergraduates and I want them to learn their way through eclipse. But the small silly programs that ask you to input characters have weird behaviour, if you use the eclipse console. It seems like it groups all input and output commands and executes them together...for example...
the following program:
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
has the following output:
4
Gimme a number: The number you entered was 4
Pretty normal output on any console you'll find, not just with eclipse's one
instead of
Gimme a number: 4
The number you entered was 4
To obtain this output, you have to flush stdout before scanf'ing the number. The output is flushed either implicitely when a newline character is echoed on the console (printf("/n")) or explicitely with fflush(stdout);
to get the output you wanted use this program :
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
fflush(stdout);
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
转自:http://dev.eclipse.org/newslists/news.eclipse.tools.cdt/msg08763.html
I teach C to first year undergraduates and I want them to learn their way through eclipse. But the small silly programs that ask you to input characters have weird behaviour, if you use the eclipse console. It seems like it groups all input and output commands and executes them together...for example...
the following program:
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
has the following output:
4
Gimme a number: The number you entered was 4
Pretty normal output on any console you'll find, not just with eclipse's one
instead of
Gimme a number: 4
The number you entered was 4
To obtain this output, you have to flush stdout before scanf'ing the number. The output is flushed either implicitely when a newline character is echoed on the console (printf("/n")) or explicitely with fflush(stdout);
to get the output you wanted use this program :
#include <stdio.h>
int main() {
int n = 0;
printf("Gimme a number: ");
fflush(stdout);
scanf ("%d", &n);
printf("/nThe number you entered was %d/n", n);
return 0;
}
转自:http://dev.eclipse.org/newslists/news.eclipse.tools.cdt/msg08763.html
相关文章推荐
- eclipse中的关于scanf和printf的输入顺序的解决办法
- 关于eclipse cdt无法正常使用printf和scanf的解决办法
- eclipse中scanf和printf的运行顺序颠倒的解决方法
- 关于mysql数据库在输入password后,滴的一声直接退出界面的解决的方法(具体办法)
- 关于在运行qt的程序时提示无法定位程序输入点_ZdaPvj于动态链接库Qt5Cored.dll”的解决办法
- 关于Ubuntu13.10运行eclipse时菜单无法使用的解决办法
- 关于Eclipse使用Ctrl键卡顿的原因及解决办法
- 关于tk2d新UI中的Input控件,因摄像机的size变大而无法输入的解决办法
- 关于在Eclipse中部署时webapps目录下没有web应用程序的解决办法
- eclipse项目中关于导入的项目里提示HttpServletRequest 不能引用的解决办法
- 关于eclipse新建web项目,提示:"The superclass "javax.servlet.http.HttpServlet" was not found on the Java"解决办法
- Linux 标准 输入 输出 重定向. printf 不输出到屏幕的解决办法
- 关于eclipse android 开发加载图片资源提示找不到对应名称解决办法
- 关于Eclipse 自带javadoc导出 api 问题解决办法(Doxygen)
- C中的scanf不等待输入的原因及解决办法
- 关于eclipse提示:"The superclass "javax.servlet.http.HttpServlet" was not found on the Java"解决办法
- 【吐血写此博】Fedora14下eclipse进行android开发,ibus提示没有输入窗口的 解决办法
- 关于eclipse中adt插件不能启动的问题的解决办法
- 关于在eclipse中创建servlet不能自动添加web.xml文件的解决办法
- 关于SecureCRT输入无显示的问题解决办法