How to Create and Run a Program in C++

To create a program in C++, you need to have two things on your computer:

  • A compiler to translate the code a computer can understand (GCC for Linux/macOs or MinGW for Windows).
  • A text editor to write a program (Visual Studio Code).

Throughout this tutorial, we will use Visual Studio Code as a text editor for writing programs and the MinGW C/C++ compiler to compile them.

To create and run your first program in C++, follow these steps:

  1. Create a project folder in your preferred workspace directory using the terminal command mkdir project_name. For example:
  2. mkdir my_project
  3. Add your newly created project folder to the Project Explorer in the Visual Studio Code IDE by going to the main menu, choosing File > Add Folder to Workspace, and then choose your project folder.
  4. When your project appears in the Project Explorer, right-click the project folder and choose New File. Enter a file name of your choice with a .cpp extension, and then press Enter to create the file. For example, main.cpp.
  5. Copy the code below to the .cpp file you created earlier:
  6. #include <iostream>
    using namespace std;
    int main() {
      cout << "Hello, World!";
      return 0;

    This is a simple program that prints "Hello, World!" in the console. In this example, we have included the iostream header file. This header file contains functions for handling input and output streams in C++. The std namespace is the standard namespace that includes built-in classes and functions. The main() function is the program's main entry point. To execute a program in C++, we must call this function. The main function is declared with int type, which signifies that our program should return an integer value. We achieve this by returning 0 at the end of the main function. The return 0 statement indicates that the program should return 0 when execution reaches that point in the code. To print output to the console, we use cout. In C++, cout is an object of the iostream class used for displaying output in the console.

  7. Save the file.
  8. To run the program, right-click the .cpp file and choose > Run Code from the menu item.
  9. If the code is valid, you should see the output like this:
  10. Hello, World!