How to Set up Visual Studio Code for C and C++ Programming 2025
Table of Contents
Introduction
This tutorial will guide you through the process of setting up Visual Studio Code (VSCode) for C and C++ programming on Windows. By following these steps, you'll be able to install the necessary tools and extensions to compile and run C/C++ code directly from VSCode.
Step 1: Download and Install MinGW
MinGW (Minimalist GNU for Windows) is a lightweight C/C++ compiler that you need to install first.
-
Visit the MinGW website:
- Go to MinGW.
-
Download the installer:
- Click on the "Download" option to get the MinGW installation manager.
-
Run the installer:
- Follow the prompts to install MinGW. Ensure you select the following packages during installation:
mingw32-base
(Basic MinGW)mingw32-gcc-g++
(C++ Compiler)
- Follow the prompts to install MinGW. Ensure you select the following packages during installation:
-
Set up the system path:
- After installation, add MinGW to your system path:
- Right-click on "This PC" or "My Computer" and select "Properties."
- Click on "Advanced system settings."
- In the System Properties window, click on "Environment Variables."
- In the "System variables" section, find the "Path" variable and click "Edit."
- Add the path to the MinGW
bin
directory (usuallyC:\MinGW\bin
). - Click "OK" to save changes.
- After installation, add MinGW to your system path:
Step 2: Download and Install Visual Studio Code
Next, you need to install Visual Studio Code, which is a lightweight code editor.
-
Go to the Visual Studio Code website:
- Visit VSCode.
-
Download the installer:
- Click the "Download" button for Windows.
-
Install VSCode:
- Run the downloaded installer and follow the prompts to complete the installation.
Step 3: Install the Code Runner Extension
The Code Runner extension allows you to run code snippets directly from VSCode.
-
Open Visual Studio Code:
- Launch the application after installation.
-
Access the Extensions view:
- Click on the Extensions icon on the sidebar or press
Ctrl + Shift + X
.
- Click on the Extensions icon on the sidebar or press
-
Search for Code Runner:
- In the search bar, type "Code Runner" and locate the extension by Jun Han.
-
Install the extension:
- Click on the "Install" button to add Code Runner to your VSCode.
Step 4: Configure VSCode for C/C++
Now, you need to set up the editor to properly compile and run C/C++ code.
-
Open the Command Palette:
- Press
Ctrl + Shift + P
to open the Command Palette.
- Press
-
Search for and select "C/C++: Edit Configurations (UI)":
- This will allow you to create a configuration file for compiling C/C++ code.
-
Configure the settings:
- In the configurations UI, set the following:
- Compiler path: Point to the
g++.exe
location (usuallyC:\MinGW\bin\g++.exe
). - Add any necessary flags or options you wish to use for compilation.
- Compiler path: Point to the
- In the configurations UI, set the following:
-
Save your configuration:
- Make sure to save any changes made.
Step 5: Create and Run a C/C++ Program
You are now ready to write and run your first C/C++ program.
-
Create a new file:
- Click on "File" > "New File" and save it with a
.cpp
extension for C++ (e.g.,hello.cpp
).
- Click on "File" > "New File" and save it with a
-
Write a simple program:
#include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; }
-
Run your program:
- To execute the code, either:
- Right-click inside the editor and select "Run Code," or
- Press
Ctrl + Alt + N
to run the code via Code Runner.
- To execute the code, either:
Conclusion
You have successfully set up Visual Studio Code for C and C++ programming on Windows. You installed MinGW, VSCode, and the Code Runner extension, allowing you to write and execute C/C++ code seamlessly. As a next step, consider exploring additional VSCode extensions tailored for C/C++ development or delve into more complex programming projects to enhance your skills. Happy coding!