Using scanf in vscode (c language) - c

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?

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:

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.

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

Related

vscode keeps telling me that the make function doesn't exist and I'm confused

I'm on the first CS50 lecture and I'm entering the code into VS Code exactly how I thought it was supposed to be, but for some reason I keep getting the errors at the bottom. It keeps telling me when I try to run make helloweb that the make function doesn't exist 😪
Here's the code.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
string answer = get_string("What's your name? ");
printf("Hello, answer\n");
}
I fixed this issue by running cs50 through Codespaces.
Go to https://code.cs50.io/ and log in with GitHub. After that, you should be able to run Codespaces through VS Code natively by installing the codespaces extension for VS Code. Finally, launch https://code.cs50.io/ through a browser and click "Open in VS Code Desktop".

Why does my C code run in terminal but not in VS Code using Code Runner?

During a class we started learning C, all of the tasks that I was given I managed to do expect when the scanf tasks came, after I couldn't make it work, I copied the code from the professor and I still couldn't make it work.
I am using vs code, code runner extension and gcc on ubuntu linux.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b;
scanf("%d", &a);
scanf("%d", &b);
printf("Zbir je %d\n", a+b);
return 0;
}
Here is the output, it does not stop untill I press the stop button:
Output
The code looks fine. You might want to change a setting in vs-code to run code in an integrated terminal.
Go to File ->Preference -> Settings. Add the following code:
{
"code-runner.runInTerminal": true
}
I tried running it in my machine and it ran correctly.
I can tell from the picture that you are using vscode and running it with an extension. The problem must be there

#Include <stdio.h> problem (Visual Studio Code)

I am trying to do a project in C but I have problems with the string #include<stdio.h>, I tried several tutorials but none of them worked, my code:
#include <stdio.h>
int main () {
int age;
printf ("Enter age:");
scanf ("% d", age);
printf ("age is% d", age);
return 0;
}
The problems are 2:
#Include errors were encountered. Update includePath.
Unable to open source file error code "stdio.h".
For this project I have created a folder, in which there is a folder called .dist and my Main.c file
(I have attached an image to be clearer)
Could anyone tell me how I can solve this problem?
The error showed in the first picture is due to the incorrect setting of the IntelliSenseMode of the editor. I bet that you have installed gcc compiler on your computer and you should also set "gcc" on the IntelliSenseMode of the editor which, by default, has a wrong value that freakout when it reads "#include <stdio.h>".
Thank you very much for all the advice, I added the files c_cpp_proprietes.json, launch.json, task.json and I added the Microsoft C / C ++ extension, I also fixed the code, but the problem remains before, also because I don't know what code I have to put in the files that I added maybe?
First, go to your C/C++ extension configuration and change your compiler path to gcc.exe
Then change Intellisense mode to windows-gcc-x64
Attached a screenshot for better understanding.
[VS Code
So there are 2 problems here:
1st: In the 7th and 8th line of code you have written "% d" which should be "%d" to define the integer data type.
2nd: Again in the 7th line of code you forgot to put "&" before the variable "age" which provides the address of the variable to put the entered value.
For better understanding attaching image.

Code wont compile or throw an error when using scanf in C, gets stuck building forever

I want to preface this with the information that I am pretty inexperience with coding.
Whenever I try to compile my code, it never finishes building and never throws an error. I then have to use Task Manager to stop stuck.exe (stuck is the name of the c file) so that I can try to compile again. I have narrowed down the issue to having something to do with the scanf function.
#include <stdio.h>
int main(void) {
int number = 0;
printf("this line shouldn't break anything. number = %d\n", number);
printf("what should the new value of number be?: ");
scanf("%d", &number);
return 0;
}
When I remove the line that has the scanf function, the rest of the code compiles as it should.
I am doing all of this in SublimeText3 on Windows 10 and using GCC provided by MinGW.
Any information you can give to help me would be appreciated, and If you would like any more information please let me know.
If you have a process stuck.exe, it means that the program finished compiling and was automatically started by the IDE/text editor. scanf reads from standard input, but apparently the IDE does not execute it in an interactive fashion, so that you cannot enter the number via the IDE.
In your IDE, you need to use the Compile or Build command (and not Run), and invoke stuck.exe manually in a command shell window.
Even my compiler is GCC-MinGW and i use Vscode, And your program works just fine even with online compilers.Maybe there is a problem with your C installation or check if your system memory near to full it might cause problems like these sometimes.

Why does Eclipse fail on this scanf() command when the Command Prompt executes it fine?

I'm new to C. Here's my code:
/* Using scanf() */
#include <stdio.h>
int main(void) {
int iDec1, iDec2, iDec3;
printf("Enter three decimals:\n");
scanf("%d,%d,%d", &iDec1, &iDec2, &iDec3);
printf("Your decimals are %d, %d and %d.", iDec1, iDec2, iDec3);
return 0;
}
It works in the Command Prompt, but when I run it through Eclipse it doesn't do anything. After hitting stop, this appears in the Console output:
Enter three decimals
Your decimals are 3, 2147344384 and 2147344384.
What the...? How come it works fine outside Eclipse but not inside Eclipse?
So, this thread might help you out. Yes, it's for Java, not C, but the last post on this thread outlines how to get input to work in eclipse console. This may just come down to how you run your program.
If the info in the link doesn't help, please post the steps you take to execute your program (which menu options you use etc. Also post eclipse version). I'll try to replicate.
fflush(stdout); did the trick.
you can use fflush(stdout); after the print cmd so it will refresh and u will get your input screen.

Resources