1

I am learning the C language on macOS using the VS Code IDE.

Below is the C code I am trying to run.

#include <stdio.h>

int main(void)
{
    int number_from_user;

    /* Get number from user */
    printf("Please enter month number: ");
    scanf("%d", &number_from_user);

    /* Print month name */
    switch (number_from_user)
    {
        case 1:
            printf("January");
            break;
        case 2:
            printf("February");
            break;
        case 3:
            printf("March");
            break;
        case 4:
            printf("April");
            break;
        case 5:
            printf("May");
            break;
        case 6:
            printf("June");
            break;
        case 7:
            printf("July");
            break;
        case 8:
            printf("August");
            break;
        case 9:
            printf("September");
            break;
        case 10:
            printf("October");
            break;
        case 11:
            printf("November");
            break;
        case 12:
            printf("December");
            break;
        default:
            printf("Not a month");
            printf("Please run the program again");
            break;
    }
}

Below is the launch.json file.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",
            "preLaunchTask": "C/C++: gcc build active file"
        }
    ]
}

The problem that I am facing is, to make the scanf function to work, I need to set externalConsole to true. Unfortunately, when I run the code, it does open the terminal but it opens a blank terminal and doesn't actually run the code.

What am I doing wrong and how do I fix this?

7
  • The code itself is fine although a bit awkward an naive. Try to replace printf("Please enter month number: ") with printf("Please enter month number:\n"). It this works, then it's a line buffering issue. If not, the problem is within your environment. Commented Jan 21, 2021 at 9:24
  • Adding \n doesn't fix it. Definitely a problem with my environment. Commented Jan 21, 2021 at 9:27
  • Does a simple "Hello World" program work? Commented Jan 21, 2021 at 9:30
  • 1
    When I set externalConsole to false, printf works fine but scanf doesn't apparently because VSCode's debug console cant take in user inputs. So I set externalConsole to true, and when I do this nothing works. Commented Jan 21, 2021 at 9:39
  • As the actual C code is not relevant to the question, your question is more likely to be answered if you simplify your code in order to have a minimal reproducible example. Commented Jan 21, 2021 at 9:42

1 Answer 1

1

I installed the plugin code-Runner, normally if you get the gcc already installed and working would be working at the moment you install it.

https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

You just click the play (or run) button, it will opens automatically a console inside the VS-Code and run the actual program.

If the problem is only with scanf persists, you can try this Go to File > Preferences -> User Settings and add custom settings:

{
    "code-runner.runInTerminal": true
}

Here's the source of the explanation: https://github.com/formulahendry/vscode-code-runner/issues/72

Ps: Be careful, only accepts single file programs, in the beginning is useful, but maybe later would change to others more complex.

Sign up to request clarification or add additional context in comments.

3 Comments

@Jabberwocky thx for the edit! Still need some "sense of style"
Thanks for this answer, unfortunately, this doesn't work for me. When I press the run button, code runner states that the code is running, but no terminals open. Settings up VSCode for C is a bit of a nightmare, I think I might just use Xcode as it fully supports C
platzi.com/tutoriales/1469-algoritmos/… @SidS try to follow this guide, is the one that I used to install it time ago, and the console just open normally. If the problem still goes, I'm sorry I can't help you without the PC in the hand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.