#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;
}
本文介绍了解决Eclipse环境中使用C语言编程时遇到的scanf和printf输入输出顺序问题。通过在scanf前使用fflush(stdout)来刷新输出缓冲区,可以确保程序按预期顺序接收用户输入。
1793

被折叠的 条评论
为什么被折叠?



