0

I'm new to C programming and whenever I try to run a code with the scanf it doesn't execute.

#include <stdio.h>
int main() 
{
int a,b,s;
printf("Enter the first no: \n");
scanf("%d",&a);
printf("Enter the second number: \n");
scanf("%d",&b);
s=a+b;
printf("%d",s);
}

I get this output:

[Running] cd "c:\msys64\mingw32\bin\" && gcc Prog1.c -o Prog1 && "c:\msys64\mingw32\bin\"Prog1

and this upon termination:

[Done] exited with code=1 in 6.586 seconds

The exact same code works without scanf.

#include <stdio.h>
int main()
{
int a=3,b=5,s;
printf("Enter first no: \n");
printf("Enter the second no: \n");
s=a+b;
printf("%d",s);
}

Output:

Enter first no: 
Enter the second no: 
8

This is what happens on executing in cmd prompt:

[1]: https://i.stack.imgur.com/o08a9.jpg
Garrisson
  • 1
  • 1
  • 2
    Isn't it simply waiting for you to type something? – Thomas Apr 26 '22 at 14:45
  • 1
    What does "doesn't execute" actually mean? What behavior of VSC or of the program do you observe? – John Bollinger Apr 26 '22 at 14:45
  • Also, what if you run the program from the command line instead of inside VSC? – John Bollinger Apr 26 '22 at 14:47
  • You `cd "c:\msys64\mingw32\bin\"` then `gcc Prog1.c -o Prog1`. Is `Prog1.c` in `c:\msys64\mingw32\bin` ? – 001 Apr 26 '22 at 14:49
  • Might be related to this previous question: [Visual Studio Code: Take Input From User](https://stackoverflow.com/questions/36964949). – Steve Summit Apr 26 '22 at 14:58
  • What input do you type? How do you run and terminate that program? – Gerhardh Apr 26 '22 at 15:05
  • @Thomas nope. It doesn't even print the first statement. – Garrisson Apr 26 '22 at 16:13
  • @Gerhardh It doesn't let my type any input. I run and terminate the programme using the buttons on the top right. – Garrisson Apr 26 '22 at 16:15
  • @JohnnyMopp yes – Garrisson Apr 26 '22 at 16:25
  • @Garrisson Please go read [this question and its answers](https://stackoverflow.com/questions/36964949). Do those help? – Steve Summit Apr 26 '22 at 16:35
  • @SteveSummit I've checked run in terminal and it says the command gcc was not found but does exist in the current location. If you trust this command, type .\gcc in the command. I tried the .\gcc line in the command line but it still gave no output – Garrisson Apr 26 '22 at 17:00
  • @Garrisson I suspect you have a VS Code problem, not a C problem. I don't know anything about VS Code, but that question I pointed you to seems to be quite common. There's nothing wrong with your C code, that's for sure. You might want to post a question tagged 'vscode', to get help from people who know that IDE and its quirks. – Steve Summit Apr 26 '22 at 17:30

0 Answers0