printf() 在 do while 循环之前不起作用

时间:2021-04-06 05:24:37

标签: c eclipse codeblocks

我尝试运行此代码:

printf("Enter a character.");
printf("\n");

do {
    ch = getch();
    system("cls");
    putchar(ch);
} while(ch != '.');

以上代码在 Codeblocks 上运行良好,但在 Eclipse IDE 上不起作用。事实上,printf 循环之前的 do while 语句不起作用,但如果我禁用 do while 语句,printf 语句在 eclipse 上运行良好。你能解释一下为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

也许试试这个?

printf("Enter a character.\n");
fflush(stdout);

do {
    ch = getch();
    system("cls");
    putchar(ch);
} while(ch != '.');

我认为这是因为您的输出缓冲区 (printf) 未刷新。