c - Eclipse CDT 和 getch()

标签 c eclipse-cdt getch

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

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

#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/

相关文章:

c - 删除基于结构的数组中的最后一个单元格

c - 数组初始化期间的类型

c - 释放c中的单向链表

eclipse-cdt - 将第三方库源文件导入 Eclipse CDT

c - x86 程序集从 32 位寄存器中获取一个字节

python-3.x - 如何在一行中使用 getch() 进行打印?

c - 在C程序中在txt文件的一行中搜索2个不同的单词

java - 如何查看我的 Eclipse 版本?

linux - 如何调试需要以 root 用户身份从 gdb (Eclipse) 运行的程序(设置 gdb suid root?)

c++ - 大多数代码不只显示欢迎消息和 “enter pin”吗?