Sunwoo Jeong
Sunwoo Jeong

Reputation: 117

Using scanf in VS Code with the Code Runner extension

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?

Upvotes: 3

Views: 4675

Answers (3)

Harold Gilchrist
Harold Gilchrist

Reputation: 1

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

Upvotes: 0

Rex Wang
Rex Wang

Reputation: 81

I just came across the same problem. In my case, just search "code-runner.run" in the setting and check the box as follow.

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

Image instruction: screenshot

Upvotes: 8

zhangxaochen
zhangxaochen

Reputation: 34017

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.

Upvotes: 0

Related Questions