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"Exemplobut nothing happens.
Can someone help me?
2
Answers
Instead of running the code from the IDE (such as picking
Start Debugging
orStart 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:or just
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.
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 whenscanf()
is called so theprintf()
prompt text is not output, pending a newline or afflush()
.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.