skip to Main Content

Im currently learning c, and I downloaded all necessary files for c to work on Vscode, if i run a code that doesnt have scanf, it runs normally, however, when I run a code that has scanf in it, it simply says "Running"

These are the extentions I downloaded

This is the code:

#include <stdio.h>

int main()
{
    int num;
    printf("Insert number");
    scanf("%d", &num);
    printf("%d",num);
    return 0;
}

When i compile and run, it says this:

[Running] cd "c:UsersLENOVODesktopProgramaçãoC" && gcc Exemplo.c -o Exemplo && "c:UsersLENOVODesktopProgramaçãoC"Exemplo

but nothing happens.
Can someone help me?

2

Answers


  1. Instead of running the code from the IDE (such as picking Start Debugging or Start Without Debugging), open a terminal (either within VSCode or a cmd shell), navigate to the directory where the code was built, and run the program from the command line:

    > cd c:UsersLENOVODesktopProgramaçãoC
    > Exemplo
    

    or just

    > c:UsersLENOVODesktopProgramaçãoCExemplo
    

    I just played with it in VSCode on my Mac and got the same behavior — it looks like there’s an issue when trying to run an interactive C program from the IDE.

    Login or Signup to reply.
  2. The scanf() is likely not the issue here. The problem is more likely that the VSCode stdout redirection is such that the stdout stream is not flushed as it should be when scanf() is called so the printf() prompt text is not output, pending a newline or a fflush().

    This is probably peculiar to the execution environment provided by VSCode, but I have seen this in other IDEs and non-hosted embedded systems. You can work around it by explicitly flushing stdout. Whilst that should not be necessary, but does no harm.

    printf("Insert number: ");
    fflush( stdout ) ;
    scanf("%d", &num);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search