我在 arch linux 中使用 neovim 和 gcc C 编译器,这就是我在 .vimrc 中使用的来编译和运行
map <F5> :w <CR> :!gcc % -o %< && ./%< <CR>
问题是我的代码可以正常运行,但任何 scanf() 函数都不会提示输入,并且在程序运行时将被忽略。即使使用 vim 编译然后在单独的 zsh 终端中运行后,它也将允许我在使用 ./x
运行代码时输入值。
我提前道歉,我是 vim 新手,想用它来加快我的工作流程。
以下代码显示了该问题:
#include <stdio.h>
int main()
{
char Team1[20];
char Team2[20];
int team1Score, team2Score;
printf("Please enter the name of team one: ");
scanf("%s", Team1);
printf("Please enter the name of team two: ");
scanf("%s", Team2);
printf("Please enter the score for %s: ", Team1);
scanf("%d", & team1Score);
printf("Please enter the score for %s: ", Team2);
scanf("%d", & team2Score);
if (team1Score > team2Score)
{
printf("%s scores 3 points and %s scores 0 points", Team1, Team2 );
}
else
if (team1Score < team2Score)
{
printf("%s scores 3 points and %s scores 0 points", Team2, Team1 );
}
else
{
printf("Both %s and %s score 1 point", Team1, Team2);
}
return 0;
}
最佳答案
问题可能不在于你的程序,而在于 vim 执行它的方式。如果您查看 :!
命令的文档,您可以看到以下内容:
The command runs in a non-interactive shell connected to a pipe (not a terminal).
非交互式 shell 是指不允许输入用户命令的 shell。你的程序不会从终端读取 scanf 输入,而是从 vim 创建的管道读取。
如果您使用的是最新版本的 vim(8.0 或更高版本,如果我是对的)或 neovim,那么您可以使用 :term
命令打开终端。在该终端中,您将能够输入用户输入。
关于c - 为什么在 Vim 中运行 C 代码会跳过 scanf()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54650814/