简体   繁体   中英

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). 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.

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 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. I installed Ming from Sourceforge. When I went back and reviewed the directions for VSC, I noticed they recommended I download from MSYS2. So I removed the original installation and re-installed from MSYS2. All my problems with scanf went away. So my advice is to follow the installation instructions from the VSC website. https://code.visualstudio.com/docs/cpp/config-mingw

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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