gpt4 book ai didi

c - Eclipse CDT 和 getch()

转载 作者:太空宇宙 更新时间:2023-11-04 04:37:49 25 4
gpt4 key购买 nike

我尝试用谷歌搜索我的问题的答案,但我似乎找不到。

这是我非常简单的测试代码:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void) {
char ch;

printf("Enter character: ");
ch = getch();
printf("%c", ch);

return 0;
}

当我尝试在 Eclipse 中运行它时,我什至无法显示第一个 printf 行,并且执行任何按键都没有任何效果。

我也尝试过执行 fflush(stdout) 和 fflush(stdin),但程序并不像我想要的那样。如果我在 Visual Studio 上尝试这个,它会完美运行。

有人知道为什么吗?谢谢。

最佳答案

output, for instance to the console/terminal, is buffered.   
it will not actually be output until either:
1) a newline is output.
2) fflush(stdout) is called.
3) a read from stdin is performed

using getchar() will cause the stdout output buffer
to be flushed to the console/terminal.

the final printf() is not showing for this same reason.
suggest changing the format string from "%c" to "%c\n"

关于c - Eclipse CDT 和 getch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29313325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com