Running a C program in Visual Studio Code (or VS2019) - c

I've just downloaded VS Code, and am struggling to work out the basics of running any programs. I'm attempting to run a basic Hello World program in C:
#include<stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
I found out that I needed to download a C compiler to run C programs on VS-Code, so I downloaded Visual Studio with C++ from the Microsoft website, which basically installed Visual Studio 2019. The heading on the link said "Develop C and C++ applications", so I figured it would at least let me program in C.
After doing this, in VS-Code I downloaded the C/C++ extension from Microsoft, and the Code Runner extension by Jun Han, which was recommended by a YouTube video. I then selected the Run Code (...environment? application?), and pressed the Play button on the top-right. The output in the terminal was:
[Running] cd "c:\Users\Leo\Documents\VSC Test Project\" && gcc test.c -o test && "c:\Users\Leo\Documents\VSC Test Project\"test
'gcc' is not recognized as an internal or external command,
operable program or batch file.
[Done] exited with code=1 in 0.078 seconds
I don't understand the error - so I opened Visual Studio 2019 to see if I could develop C applications in that. But no, only C++. Did Microsoft lie to me, or is there something else I need to do to configure C in VS2019?
Basically, I have no idea what I'm doing in VS-Code, and would really appreciate someone taking the time to break down how to actually run a program in there, and get C working. Or if there's a simple solution that allows me to write C in VS-2019, that would be equally great.

Related

i am trying to run a hello world program in c in visual studio code but its not working

this is my program
#include stdio.h
int main(){
printf("hello world");
return = 0;
}
when I run it in vs code it says
'gcc' is not recognized as an internal or external command,
operable program or batch file.
what is broken and how do I fix it
I do have the c/c++ extension installed, I also have code runner installed
I also have looked around the internet for a solution, but none of them really made sense to me when I searched for them i copied and pasted the error message
You need to install either the GCC or G++ compilers. Assuming you are on linux, it's sudo apt install gcc.

How can i setup Visual Studio Code for simple C programming?

Completely new to programming and will start a mechanical engineering course this year and we have to set up a C enviroment with Visual Studio Code at home.
Was following the instructions so i downloaded and installed MinGW and set the path in Windows. Tested this with the cmd console. Then installed VSC and C/C++ extension and i was supposed to test it with the "Hello World!" program but i get an error message every time:
launch: program 'D:\folder\build\Debug\outDebug' does not exist
Was searching and found that it had something to do with the launch.json config file so i have rewritten it with the suggested ${workspaceFolder} instead of the \ pathing but i get the same error.

Why does Windows Defender detect compiled C executables as viruses?

I'm using Visual Studio 2019 (Community Edition). I've never had an issue when compiling C++ code. I simply get the final executable and I'm able to run it without a problem. Recently I've decided to learn C and this is the C program that I'm compiling:
#include <stdio.h>
int main()
{
printf("Hello, world! \n");
return 0;
}
The file name is "main.c".
I haven't changed any of the default project settings and the project that I selected was "empty C/C++ project".
The code compiles fine but when I attempt to run the executable I get this error:
Unable to start program [path to .exe file]
Operation did not complete successfully because the file contains a
virus or potentially unwanted software.
I can resolve this issue by going to Windows Defender and manually allowing this detected threat but obviously the issue with this approach is that other people won't be able to run my C programs without them being detected as a virus.
Ultimately, I believe this problem is related to how VS2019 is compiling my program rather than Windows Defender because I'm able to compile C++ (and other languages) into executables that run perfectly fine.

How to use VC++ compiler in command line(Windows)?

I tried to use VC++ commandline, instead of MinGW compiler for windows system programming. I wrote a simple hello world program and tried to compile it, then i got this error message.
test2.c(1): fatal error C1083:'stdio.h': No such file or directory
I also added "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\bin\Hostx86\x86" directory (where c1.dll lives) to the System Envionment Variable(PATH).
How can I fix this issue? Other tutorials don't give much information about VC++. (A lot of MinGW compiler tutorial out there btw)
You should use visual studio command line if you want to compile or run program with the help of vc++ compiler.
Else all information related to setting environment variables ETC. resides in this MSDN document.

Can't compile C on windows 7 x64

I have visual studio 2012 ultimate and mingw installed on my pc.
However, when I try to compile any program (I have only tried a simple hello world program)
e.g when I use the visual studio cl compiler or the gcc compiler I get the error :
cl is not recognized as an internal or external command, operable program or batch file.
and similar for mingw-gcc.
I have visual studio installed in the default location. I have installed mingw in D:\MinGw but I have added it to the path variable. Also I have installed CodeBlocks after getting frustrated with this and my program builds and runs easily when I try that from inside CodeBlocks. I can also compile my program from inside Visual Studio by clicking on the build and run buttons.
What do I need to do to compile and run programs from the commandline in windows 7?
Instead of running a "regular" command prompt, run the "Visual Studio command prompt". You can find a link to it in the start menu.
It's essentially the same, but it sets up for you all the environment variables you need for all the VS tools to work from the command line.
Sounds like you still have a path problem. The mingw-gcc path for example would need to be to the bin directory:
D:\MinGW\bin
in your case. Then you should be able to run gcc.

Resources