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:

  1. C or C++ source code: The program you want to execute.
  2. Visual Studio Code: The code editor used to write and manage your files.
  3. A compiler: GCC compiles C programs, while G++ compiles C++ programs.
  4. A terminal: Used to run compiler commands and view program output.
  5. 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.

  1. Visit the official Visual Studio Code website.
  2. Download the installer for Windows.
  3. Run the installer and follow the setup instructions.
  4. 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

  1. Visit the official MSYS2 website.
  2. Download the Windows installer and run it.
  3. Keep the default installation location unless you have a specific reason to change it.
  4. Finish the installation and open the MSYS2 UCRT64 terminal from the Start menu.
  5. Update the package database and installed packages when prompted.
  6. 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

Example03
C/MSYS2 UCRT64 terminal
1pacman -Syu
2 
3# If instructed, close and reopen the terminal,
4# then complete any remaining updates.
5 
6pacman -S --needed mingw-w64-ucrt-x86_64-gcc

🔧 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

  1. Open the Windows Start menu and search for Edit environment variables for your account.
  2. Open the Environment Variables settings.
  3. Under User variables, select Path and click Edit.
  4. Click New and add C:\msys64\ucrt64\bin.
  5. Click OK to save the changes in all open dialogs.
  6. 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.

Example04
CPATH entry
1C:\msys64\ucrt64\bin

✅ 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.

Example05
CTerminal commands
1gcc --version
2g++ --version

🧩 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.

Example06
CExtensions to search for
1C/C++ — Microsoft
2Code Runner — optional

📁 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.

Example07
C/hello.c
1#include <stdio.h>
2 
3int main() {
4 printf("Hello, World!\n");
5 return 0;
6}

▶️ 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.

Example08
CCompile
1gcc hello.c -o hello.exe
CRun in the VS Code terminal
1./hello.exe
CExpected output
1Hello, World!

📝 Step 8: Create a C++ Program

Create another file named hello.cpp in your project folder. Use the following C++ program.

Example09
C/hello.cpp
1#include <iostream>
2using namespace std;
3 
4int main() {
5 cout << "Hello, World! I am learning C++." << endl;
6 return 0;
7}

▶️ 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.

Example10
CCompile
1g++ hello.cpp -o hello_cpp.exe
CRun in the VS Code terminal
1./hello_cpp.exe
CExpected output
1Hello, World! I am learning C++.

🚀 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.

Example11
CRecommended Code Runner settings
1Code Runner: Run In Terminal = Enabled
2Code Runner: Save File Before Run = Enabled

🛠️ 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.

Example12
CCheck compiler
1gcc --version
2g++ --version
CCompile C
1gcc hello.c -o hello.exe
CCompile C++
1g++ hello.cpp -o hello_cpp.exe

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8