To run a C program in Visual Studio Code, follow these steps:
- Install the C/C++ Extension: Ensure you have the C/C++ extension installed in Visual Studio Code. You mentioned you have this set up, which is great.
- Set Up a Workspace: Create a new folder for your C projects. Open Visual Studio Code and select
File > Open Folder...to open your newly created folder. - Create a C File: Inside your workspace folder, create a new file with a
.cextension (e.g.,hello.c). You can do this by right-clicking in the Explorer pane and selectingNew File. - Write Your Code: Write your C code in the newly created file. For example:
#include int main() { printf("Hello, World!\n"); return 0; } - Configure Build Tasks: You need to set up a build task to compile your C code. Press
Ctrl + Shift + Bto open the build tasks menu. If prompted, selectCreate tasks.json file from templateand chooseOthers. Modify the generatedtasks.jsonfile to include the following:{ "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "gcc", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [ "$gcc" ], "detail": "Generated task by gcc" } ] } - Build Your Program: Press
Ctrl + Shift + Bagain to build your program. This will compile your C code and create an executable in the same directory. - Run Your Program: Open the terminal in Visual Studio Code (
View > Terminalor `Ctrl + ``) and run your program by typing:
(Replace./hellohellowith the name of your executable if different.) - Debugging: To debug your program, you will need to set up a launch configuration. Go to the Run view (left sidebar) and click on
create a launch.json file. ChooseC/C++: g++ build and debug active file. This will create alaunch.jsonfile where you can configure debugging settings. - Start Debugging: Set breakpoints in your code by clicking in the gutter next to the line numbers. Then, press
F5to start debugging your program.
By following these steps, you should be able to set up Visual Studio Code for C programming and run your code successfully.
References:
- Walkthrough: Compile a C program on the command line
What tools do you use with Visual studio code to run C code ?
How do I set up Visual Studio Code to compile and run C code? - Stack Overflow
How do I compile and run?
C programming in Visual Studio - Stack Overflow
Videos
I followed what is said on this blog post, it takes so much time to run(or build) and sometimes just doesn't.
i've been using CodeBlocks before but didn't get comfortable with it i just don't like, it would be great if i can use VSC.
Edit: i use Windows.
I’ve been programming for years now, in multiple languages. I’ve never used an IDE or anything like vs code. I usually just use a text editor and a terminal to do all my coding.
This whole process is starting to get annoying so I downloaded vs code but I cannot get anything to work. I don’t know what I am doing. According to the tutorials, I should be able to press the debug and run button and vs code will compile and run my code, but I can’t get this to work. It’s saying I need a configuration, or that blah blah blah.
So far, I’ve only been able to get python to work. So my questions are: what is a configuration? Why do I need one? How do I get c++ to work with the push of one button?
Short answer: Yes, you need to rename .cpp files to c, so you can write C: https://msdn.microsoft.com/en-us/library/bb384838.aspx?f=255&MSPPError=-2147217396
From the link above:
By default, the Visual C++ compiler treats all files that end in .c as C source code, and all files that end in .cpp as C++ source code. To force the compiler to treat all files as C regardless of file name extension, use the /Tc compiler option.
That being said, I do not recommend learning C language in Visual Studio, why VS? It does have lots of features you are not going to use while learning C
Yes, you very well can learn C using Visual Studio.
Visual Studio comes with its own C compiler, which is actually the C++ compiler. Just use the .c file extension to save your source code.
You don't have to be using the IDE to compile C. You can write the source in Notepad, and compile it in command line using Developer Command Prompt which comes with Visual Studio.
Open the Developer Command Prompt, enter the directory you are working in, use the cl command to compile your C code.
For example, cl helloworld.c compiles a file named helloworld.c.
Refer this for more information: Walkthrough: Compiling a C Program on the Command Line
Hope this helps