【发布时间】:2019-12-27 00:12:37
【问题描述】:
我是 C 语言的新手。我想弄清楚如何使用scanf。这是我目前的代码。
#include <stdio.h>
#include <string.h>
int main() {
char lastInitial;
printf("What is your last inital?");
scanf(" %c", &lastInitial);
}
当我运行这段代码(我正在使用 VS Code)时,它显示文件正在运行,但没有任何显示。当我转到顶部再次单击运行按钮时,它说此代码已经在运行。如果我停止运行,删除 scanf 行并再次运行,文件运行并显示“你的最后一个初始值是什么?”我很困惑为什么将 scanf 添加到文件中会停止 printf 并且不允许任何用户输入。
【问题讨论】:
-
尝试在
printf的末尾添加一个 '\n'。 -
我运行了 OPs 代码,在 linux 上没有任何变化,并且运行良好。
-
在 scanf 之前执行 fflush 或者 getchar()。