Get Started with C++

This chapter will explain all what you need to know to start writing C++ code and where to find the compilar and how to run it.

To whom is this tutorial directed?

This tutorial is for those people who want to learn programming in C++ and do not necessarily have any previous knowledge of other programming languages. Of course any knowledge of other programming languages or any general computer skill can be useful to better understand this tutorial, although it is not essential.

It is also suitable for those who need a little update on the new features the language has acquired from the latest standards.

Structure of this tutorial

This tutorial is divided in to sections and each sections covered an entired topic. You can access any section directly from the section index available on the left side bar, or begin the tutorial from any point and follow the links at the bottom of each section.

Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.

One of the good way to gain experience with a programming language is by modifying and adding new functionalities on your own to the example programs that you fully understand. Don't be scared to modify the examples provided in this tutorial, that's the way to learn!

Compatibility Notes

As I mentioned ealier C++ is standardized by the International Organization for Standardization (ISO), with the latest standard version ratified and published by ISO in October 2024 as ISO/IEC 14882:2024 (informally known as C++23)

Nevertheless, the C++ language exists from a long time before (1980s). Therefore there are many compilers which do not support all the new capabilities included in ANSI-C++, especially those released prior to the publication of the standard.

Many sections include examples that describe the use of the newly acquired knowledge in the chapter. It is recommended to read these examples and to be able to understand each of the code lines that constitute it before passing to the next chapter.

This tutorial is thought to be followed with modern compilers that support -at least on some degree- ANSI-C++ specifications. I encourage you to get one if yours is not adapted. There are many options, both commercial and free. We will talk about them in the below explanition.

Compilers

As you know we have thousands of compilers available in the market both commecial and free, but here are some of the most popular ones that you can use to compile your C++ code.

Visual Studio is a powerful IDE that can be used to write, compile, test and debug C++ code. The community edition is free and includes many features. to used it you just need to add the langugage extention.

Code::Blocks is a free C, C++ and Fortran IDE built to meet the most demanding needs of its users. It is designed to be very extensible and fully configurable.

Eclipse is a free IDE that can be used to write, compile, test and debug C++ code. It is very powerful and can be used to develop complex applications.

CLion is a powerful IDE that can be used to write, compile, test and debug C++ code. It is very powerful and can be used to develop complex applications.

For beginners, the best IDE is Visual Studio Code because it is very simple and easy to use. It is also free and includes many features that will help you write, compile, test and debug your C++ code.

The below are steps on how to start working with VS Code as it is the simple and very light weight to start with.

Step 1. Choose an IDE.

As I mention earlier there are many IDEs available in the market that you can use to write, compile, test and debug C++ code. But we will stick with the most popular ones that are easy and free use; which is VS Code.

Then you need to Install a C++ extension on VS Code to enable you to write, compile, test and debug C++ code. You can install the C++ extension by following the steps below: Open VS Code, click on the 4 squares icon on the left side bar, then click on the search icon and type "C++ extention pack" and click on the install button. VS Code C++ Extention

Then after that we need another extension to enable us to run the C++ code on the terminal, to do that also click on the 4 squares icon on the left side bar, then click on the search icon and type "Code Runner" and click on the install button. VS Code Code Runner

Then also we need and environment to compile our code, to do that we need to install the MinGW compiler.

After you have downloaded it then extract the folder and copy all the content of the extracted folder to the root of your computer memory, then open the folder and find the "bin" folder when you open it then copy the address of the folder.

Search for "environment viriables" on your computer and click on it, then click on the "environment variables" button, then click on the "path" button and click on the "new" button and paste the address of the "bin" folder that you copied earlier and click on the "ok" button.

Then you can now start writing your code and run it by clicking on the run button on the top right corner of the editor.

Note: if you get error while running the code, you must close and open the VS Code again. This is because the environment variables are not updated until you restart the VS Code.

If still you are facing issues with the MinGW installation you can watch our guide on how to install MinGW on your computer by clicking on the below link MinGW installation Process

Step 2. Create a Repository or Folder.

Before starting writing code you have to create a folder where your file will going to be store in order to avoid combining your file with unwanted files. name your folder with anything you want for better identification but for the naming convention make sure to named it with useful name: like my C++ code or C++ Practical or something very useful.

Step 3. Open your chosen IDE (VS Codee).

Launch the IDE that you have chosen to use for compiling your code and locate the folder you have created, or right click on the folder and click on "open with".

Step 4. Create and Save C++ file.

Now that you have open the folder you created on your choosen IDE, to create a file move the cursor to the editor menu and click on file then new text file

After clicking "new file" the it will ask you to name it then you can name it with anything you want but must end with the extension of .cpp that is the only way the text editor will understand that you are going to write C++ code.

Note: you can give it any name you want but must end with the extension of .cpp

Step 5. Write your C++ code

Type or copy and paste your C++ code into the file you have created. You can use the below code to test your environment.

#include <iostream>
using namespace std;
int main(){
  cout<<"Hello CodingKoleji!";
  return 0;
}

After you have written your code, you can save it by clicking on the file menu and click on the save button or you can use the shortcut key Ctrl + S to save the file.

Step 6. Run your code

If you want to run the code you can click on the run button on the top right corner of the editor.

Step 5. Check your output on the console.

After you have run the code you can check the output on the console that will appear on the bottom of the editor.

If you have followed all the steps correctly you will see the output of the code that you have written on the console.

if your find it difficult to setup the environment you can try our online compiler by clicking on the below link Online C++ Compiler to test your code. It is very simple and easy to use.

Example

#include <iostream>
using namespace std;
int main(){
  cout<<"Hello CodingKoleji!";
  return 0;
}

Now that you know how to setup and run C++ code on both your machine and using our online compiler. Lets learn the basic Syntax of C++ in the next section.