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
The guide for using C++ with Visual Studio Code is located here:
- C/C++ for Visual Studio Code
If you are using the windows operating system, you can install the Microsoft Visual C++ (MSVC) compiler toolset as described here:
- Configure VS Code for Microsoft C++
Otherwise you should read the tutorials for the compiler you want to use:
- Tutorials for other compilers
The reason I refer to links, instead of explaining the details here, is because the answer might become outdated/obsolete. Sometimes links are better than explanations.
Step 1. Download the MinGW - Minimalist GNU for Windows at SourceForge, link here.
When you download Minimalist GNU for Windows (MinGW), you can choose which packages to install. You should select the following packages:
- mingw-developer-toolkit
- mingw32-base
- mingw32-gcc-g++
- msys-base
Then click on the Installation tab on the top left and click on Apply Changes.
After installation, go to your C Drive where the MinGW is installed, and enter the folders MinGW > bin. Copy the path C:\MinGW\bin
Now search for environment, and enter edit environment variables for your account. screenshot
Next, double-click on Path, click on New, paste the path C:\MinGW\bin, press enter, and click OK.
Verify that it has been installed correctly by opening up Command Prompt and typing in gcc --version. It should give you the current version number.
Step 2. In Visual Studio Code, click on the Extensions tab, search and install Code Runner by Jun Han
Step 3. In the C/C++ Configurations. Make sure the Compiler Path has c:/MinGW/bin/gcc.exe selected.
screenshot 2
You can get to it by opening Command Pallet (ctrl+shift+p) typing in C/++: Select a Configuration.. then select Edit Configurations (UI)
What tools do you use with Visual studio code to run C code ?
How to compile C program in Visual Studio Code? - Stack Overflow
How do I make vs code put the output of my c program in TERMINAL panel? - Stack Overflow
Running a simple C program using VS code - 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.
You need to add an & symbol before the variable name while scanning. Check why here.
scanf("%d\n", &age);
Working Code
#include <stdio.h>
int main()
{
int age;
printf("Please enter your age: ");
fflush(stdout); // Prints to screen or whatever your standard out is
scanf("%d", &age);
printf("Your age is: %d\n", age);
fflush(stdout); // Prints to screen or whatever your standard out is
return 0;
}
I run this and it is working as expected. If you are using linux os, please follow below command to compile and run.
gcc age.c
./a.out
Please visit here to know more about scanf function in c.
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?
