gpt4 book ai didi

c - 为什么在 Vim 中运行 C 代码会跳过 scanf()?

转载 作者:太空狗 更新时间:2023-10-29 11:40:31 24 4
gpt4 key购买 nike

我在带有 gcc C 编译器的 arch linux 中使用 neovim,这是我在 .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).

Non-interactive shell 是指不允许输入用户命令的shell。你的程序不会从终端读取 scanf 输入,而是从 vim 创建的管道中读取。

如果您使用的是最新版本的 vim(8.0 或更高版本,如果我是对的)或 neovim,那么您可以使用 :term 命令打开终端。在该终端中,您将能够输入用户输入。

关于c - 为什么在 Vim 中运行 C 代码会跳过 scanf()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54650814/

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