我正在使用Eclipse在C/C++中编写代码,我正在为一些可能相当简单的事情而苦苦挣扎。在下面的代码中,我在scanf()
之后使用了printf()
。尽管在scanf()
之前编写了printf
,但输出有所不同。我能够找到一些关于类似问题的here。但我没能解决这个问题。有什么想法吗?
代码:
#include <stdio.h>
int main()
{
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
预期输出:
Enter a number:1
1
相反,我得到的是:
1
Enter a number:1
发布于 2013-06-02 05:54:12
好的,最后我使用了类似@zsawyer写的东西作为标签为3的选项。在我的代码中,我插入了下面这行:
setvbuf(stdout, NULL, _IONBF, 0);
作为main()中的第一行:
#include <stdio.h>
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
int myvariable;
printf("Enter a number:");
scanf("%d", &myvariable);
printf("%d", myvariable);
return 0;
}
我从here那里得到的。
https://stackoverflow.com/questions/16877264
复制相似问题