简体   繁体   English

vscode中使用scanf(c语言)

[英]Using scanf in vscode (c language)

I tried a very simple code of C language using scanf, and it falls into the infinite loop(or it looks like).我使用scanf尝试了一个非常简单的C语言的代码,它陷入了无限循环(或者它看起来像)。 the code is the following:代码如下:

#include <stdio.h>

int main(){
    int input = 0;
    scanf("%d", &input);
    return 0;
}

I used the code runner extension.我使用了代码运行器扩展。 Here is the screenshot just in case.这是以防万一的屏幕截图。 enter image description here在此处输入图像描述

Does anyone know how to fix it?有谁知道如何修理它?

You can add some code to make a better appearance for users, for example:您可以添加一些代码为用户提供更好的外观,例如:

#include <stdio.h>

int main(){
    int input = 0;
    printf("Please type in something:");
    scanf("%d", &input);
    return 0;
}

In this case you can know that the code is working and it gives you some feedback.在这种情况下,您可以知道代码正在运行,它会为您提供一些反馈。 Then you can type in the value which will be taken as input before the main function returns.然后您可以在主 function 返回之前输入将作为输入的值。

I just came across the same problem.我刚刚遇到了同样的问题。 In my case, just type "code-runner.run" in the setting and check the box.就我而言,只需在设置中键入“code-runner.run”并选中该框。

Code-runner: Run In Terminal
[] whether to run code in integrated terminal.

Sorry, I am new here and can't post images:) screenshot对不起,我是新来的,不能发图片:) 截图

I had this same problem when I installed Mingw-64 and VS Code.我在安装 Mingw-64 和 VS Code 时遇到了同样的问题。 I installed Ming from Sourceforge.我从 Sourceforge 安装了 Ming。 When I went back and reviewed the directions for VSC, I noticed they recommended I download from MSYS2.当我回去查看 VSC 的说明时,我注意到他们建议我从 MSYS2 下载。 So I removed the original installation and re-installed from MSYS2.所以我删除了原来的安装并从 MSYS2 重新安装。 All my problems with scanf went away.我所有关于 scanf 的问题都消失了。 So my advice is to follow the installation instructions from the VSC website.所以我的建议是按照 VSC 网站上的安装说明进行操作。 https://code.visualstudio.com/docs/cpp/config-mingw https://code.visualstudio.com/docs/cpp/config-mingw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM