Nearby lessons
5 of 72⚙️ C and C++ Setup in VS Code
📌 Requirements for Running C and C++ in VS Code
What do you need to run C and C++ programs?
To write, compile, and run C and C++ programs in Visual Studio Code, you need the following tools:
- C or C++ source code: The program you want to execute.
- Visual Studio Code: The code editor used to write and manage your files.
- A compiler: GCC compiles C programs, while G++ compiles C++ programs.
- A terminal: Used to run compiler commands and view program output.
- Code Runner (optional): A VS Code extension that can run code using configured commands.
A compiler translates source code into a program the computer can execute. For this tutorial, we will use MinGW-w64 through MSYS2 to install GCC and G++.
💻 Step 1: Install Visual Studio Code
Visual Studio Code is a code editor where you can write and manage C and C++ programs. If it is already installed on your computer, continue to the next step.
- Visit the official Visual Studio Code website.
- Download the installer for Windows.
- Run the installer and follow the setup instructions.
- Open Visual Studio Code after installation.
Download: Visual Studio Code official website
📦 Step 2: Install MSYS2 and the GCC/G++ Compiler
MSYS2 provides development tools for Windows, including GCC and G++. GCC is commonly used to compile C programs, and G++ is used to compile C++ programs.
Installation steps
- Visit the official MSYS2 website.
- Download the Windows installer and run it.
- Keep the default installation location unless you have a specific reason to change it.
- Finish the installation and open the MSYS2 UCRT64 terminal from the Start menu.
- Update the package database and installed packages when prompted.
- Install the GCC toolchain package.
Run the following commands in the MSYS2 UCRT64 terminal:
pacman -Syu
If the terminal asks you to close and reopen it after an update, do so, then run the update command again if instructed.
pacman -S --needed mingw-w64-ucrt-x86_64-gcc
This toolchain provides both GCC and G++ executables. Do not run these MSYS2 commands in the Windows Command Prompt.
Download: MSYS2 official website
🔧 Step 3: Add the Compiler to the Windows PATH
Windows must be able to locate the GCC and G++ commands from the terminal. Add the MSYS2 UCRT64 binary folder to your user PATH.
The default path is usually:
C:\msys64\ucrt64\bin
Windows setup steps
- Open the Windows Start menu and search for Edit environment variables for your account.
- Open the Environment Variables settings.
- Under User variables, select Path and click Edit.
- Click New and add
C:\msys64\ucrt64\bin. - Click OK to save the changes in all open dialogs.
- Close any existing Command Prompt or VS Code terminal windows and open new ones.
If MSYS2 was installed in a different folder, use the actual
ucrt64\bin path from your installation.
Avoid deleting existing PATH entries. Add a new entry instead.
✅ Step 4: Verify the GCC and G++ Installation
Open a new Windows Command Prompt or VS Code terminal and run the following commands to check whether the compiler is available.
gcc --version
g++ --version
If both commands display version information, the compiler commands are available. If Windows says the command is not recognized, check the PATH entry and open a new terminal.
🧩 Step 5: Install C/C++ and Code Runner Extensions
VS Code extensions provide language support and convenient ways to run programs.
C/C++ extension
Open the Extensions panel in VS Code, search for C/C++ by Microsoft, and install it. This extension provides features such as IntelliSense, code navigation, and debugging support.
Code Runner extension (optional)
Search for Code Runner and install it if you want to run programs using a Run Code command. Code Runner is optional: you can also compile and execute programs directly from the terminal.
The C/C++ extension does not install the compiler itself. GCC and G++ must be installed separately.
📁 Step 6: Create a C Program
Create a folder for your practice programs and open that folder
in VS Code. Create a file named hello.c.
Enter the following code and save the file.
▶️ Step 7: Compile and Run the C Program
Open the VS Code terminal using Terminal → New Terminal.
Make sure the terminal is in the folder containing hello.c.
Step 1: Compile the program
gcc hello.c -o hello.exe
This command compiles hello.c and creates the executable
file hello.exe. If the compiler reports errors, correct
them before continuing.
Step 2: Run the program
./hello.exe
Expected output:
Hello, World!
Compilation and execution are separate steps when using these terminal commands.
📝 Step 8: Create a C++ Program
Create another file named hello.cpp in your project folder.
Use the following C++ program.
▶️ Step 9: Compile and Run the C++ Program
Use the g++ compiler for C++ source files.
Step 1: Compile the program
g++ hello.cpp -o hello_cpp.exe
Step 2: Run the program
./hello_cpp.exe
Expected output:
Hello, World! I am learning C++.
Use gcc for the C example and g++ for the
C++ example. Using the correct compiler helps ensure that the program
is compiled with the appropriate language settings and libraries.
🚀 Step 10: Run Programs Using Code Runner (Optional)
After installing Code Runner, open a saved C or C++ file and select Run Code from the editor's context menu or use the Run Code button if it is available.
To show interactive program input and output in the terminal, open VS Code Settings, search for Code Runner: Run In Terminal, and enable it.
You can also enable Code Runner: Save File Before Run so that changes are saved before the code runs.
If Code Runner cannot find GCC or G++, verify the PATH configuration and restart VS Code. You can always use the manual compile-and-run commands from the previous steps.
🛠️ Common Problems and Solutions
Problem 1: gcc or g++ is not recognized
Check that the compiler is installed and that the correct MSYS2
ucrt64\bin folder is present in PATH. Open a new terminal
after changing environment variables.
Problem 2: The program does not run
First compile the source file and check for errors. Confirm that the executable file was created, then run it from the correct directory.
Problem 3: The output does not show in the terminal
If you use Code Runner, enable its Run In Terminal setting. If needed, run the program manually in the integrated terminal.
Problem 4: Recent code changes do not appear in the output
Save the file before running it, or enable Code Runner's Save File Before Run setting. When compiling manually, compile the source file again after making changes.