Eclipse CDT 编译 C 程序后出现意外输出

Eclipse CDT unexpected output after compiling C Program

在使用 GCC 5.1.0 编译器在 eclipse CDT 上尝试此代码时 所有的字符串都是在用户输入后打印的.. 在 Visual Studio 和 Code Blocks IDE 上编译它时,即使使用 windows CMD 该程序也按预期工作得很好..


‪#‎include‬ <stdio.h>
static char string[128] = "";
int main() {
printf("Type a string: ");
scanf("%s",string);
printf("The String is %s", string);
return 0;
}

Eclipse 输出:


Visual Studio 输出:

谢谢,

好的,我明白了。我认为问题在于,每当您想确定代码中的给定点打印了某些内容时,您需要在该点刷新 stdout

否则,流式内容可以以依赖于实现的方式(通常是小批量)排队和传送

C标准库的printf(),当输出到stdout遇到换行符\n,提供隐式刷新,所以不需要调用flush() 你自己。而对于 C++ 的 std::cout,只有 std::endl 有这个 属性; \n 不保证。

在 C 中故意刷新 stdout 可以像这样完成:fflush(stdout);

另请参阅:Why does printf not flush after the call unless a newline is in the format string?