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

问题描述 投票:2回答:1

我在使用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;
}
c linux gcc vim scanf
1个回答
4
投票

故障可能不在你的程序中,而是vim执行它的方式。如果查看:!命令的文档,则可以看到以下内容:

该命令在连接到管道(不是终端)的非交互式shell中运行。

非交互式shell表示shell,不允许输入用户命令。您的程序不会从终端读取scanf输入,而是从vim创建的管道读取。

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

© www.soinside.com 2019 - 2024. All rights reserved.