C and C++ are widely used general-purpose programming languages. This article will show you how to run a C and C++ Program In Visual Studio Code.
Step 1: Download and install C or C++ compiler
To start writing C/C++ code in VS Code, you need to install a C and C++ compiler. The compiler you use depends on your OS.
- On Windows, you can use MinGW or Microsoft Visual C++.
- On Linux, you can use the native GCC (GNU Compiler Collection)
- On Mac, you can use Clang C/C++.
Using Windows, I will show you how to download and configure MinGW. You can skip until Step 3 if you are using Linux or Mac.
- Go to sourceforge.net/projects/mingw and download the MinGW setup.
Download the MinGW setup. - Run the setup file and select Install.
Run the setup and select Install. - Keep the default path for installation. In my case, it is C:MinGW. Select Continue.
Keep the default path and select Continue. - When the installation is complete, click on Quit.
Click on Quit after installation completion. - Launch the newly-installed MinGW application.
- Under the Basic Setup tab, select mingw32-GCC-g++.
From the Basic Setup section, choose the mingw32-GCC-g++ option. - Select Mark for Installation.
Select Mark for Installation. - From the top-level Installation menu, select Apply Changes.
In the Installation menu, select Apply Changes. - Finally, click Apply.
Click on Apply.
Step 2: Configure MinGW
Before running your C or C++ program, you must add the MinGW bin directory to your system path. Follow these steps:
- Copy the path of the bin folder inside the MinGW folder. By default, it is C:MinGWbin.
Copy the path of the bin folder inside the MinGW folder. From the start menu, search for “Path.” Then, select Edit the system environment variables option.
Search for “path” and select Edit the system environment variables. - Select Environment Variables.
Select Environment Variable. - Select Path under the “Variable” column. Then, click on Edit…
Select Path, then click on Edit. - Paste the copied path here and select OK.
Paste the copied path here, then click OK.
Step 3: Run your C and C++ Program
- Launch VS Code. Create a new source file by clicking on the New File icon. Then, type the source file name with the .c extension. I will name it HelloWorld.cNote:Alternatively, you can select the folder+ icon and select the folder which contains your C or C++ project to import it.
Create a new C file. - Write, or paste your code into the editor. For this tutorial, I will use this code to print a simple “Hello World” message:
#include <stdio.h> int main(void) { printf("Hello World!")return 0;}
Write your code. - Press Ctrl + S on your keyboard to save the file.
- To execute your code, click the Run button on the top right. Alternatively, you can press Ctrl + Alt + N.
Press the Run button or Ctrl + Alt + N to run the program. - Your code’s output will show in the terminal. You will also find a .exe file of your program in the “Explorer” view.
The result of your program is shown in the terminal console.