In this project, we’re going to write our very first program, which is essentially the first code that every programmer writes. Its sole task is to print the message “Hello, World!” onto the screen. Through this project you’ll install the necessary tools and write your very first program!
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
}Hello, World!Before we start coding, it’s essential to choose the right environment for writing our programs. Eventually, all the code we write is saved as plain text, so it is technically possible to code in NotePad (Windows), TextEdit (MacOS), or even Microsoft Word or Apple Pages, but it is going to be a nightmare. So, it’s much better to use tools that are specifically designed for programmers. We suggest using Visual Studio Code
(VSCode), which you can download and install from code.visualstudio.com.
VSCode is just an editor for writing code, and doesn’t execute the code. To run your C++ code, you need a compiler. Programs written in C++ aren’t directly executable by the computer because they need to be translated into machine code. A compiler performs this translation, converting high-level code into a format the computer can execute. The end result would be an application which you can simply run by double clicking on, like most other applications.
Installing the compiler
One development toolset with a relatively straightforward installation is MSYS2, which you can download and install from msys2.org. After installation, open MSYS2 and execute the following commands to install the compiler itself (paste them into MSYS2 using right-click and select paste or press Shift+Ins, then press Enter):
pacman -SyuRespond with ‘Y’ to any prompts. After this command, the terminal will close. Reopen it and execute:
pacman -Su
pacman -S mingw-w64-x86_64-gcc
pacman -S mingw-w64-x86_64-gdbNow, use your computer’s file explorer to navigate to the C: Drive and the msys64 folder (this is the default path, so if you changed it during the installation process go to your designated address)
Then open bin
Now click on the address bar at the top to copy the full address.

C:\msys64\mingw64\binThen, open your computer’s advanced settings

And navigate to the “Environment Variables” settings.

Then add the new address to “Path” like below:

Path is a variable that stores a list of all the locations the system should search to find terminal commands and tools. By adding this value, we include the compiler we installed into the system’s commands.
First, download and install Apple’s editor, Xcode, from the App Store.
Typically, if you follow the Xcode installation steps correctly, you’re done at this point. However, you might also need to run the following command in Terminal:
xcode-select --installIf you’re using a Debian
-based distribution like Ubuntu
, you can use the following command to install a set of tools for compiling and building programs. (It’s recommended to update the package list with apt update before installing any package.)
sudo apt install build-essentialIf you’re using Fedora or any other Red Hat
-based Linux system like CentOS
, you can use the following command to install the development tools packages:
sudo dnf -y groupinstall "Development Tools"After the installation is done, run g++ --version in VSCode
‘s Terminal to check if the GCC compiler was installed correctly:
Writing and Running the Program
Create a file from File > New Text File menu. Then, choose your language (C++ in our case) at the bottom right corner:
In VSCode, install the ‘C/C++’ extension by Microsoft, which adds a ‘Run’ button above the editor when editing C++ files. Alternatively, you can compile and run your code using the terminal in VSCode with the following command (replace <source_file> and <output_file> with your filenames):
g++ <source_file>.cpp -o <output_file> ; ./<output_file>For example, if your source file is hello-world.cpp, you would run:
g++ hello-world.cpp -o hello-world ; ./hello-worldThe first part of this command, g++ hello-world.cpp -o hello-world, compiles your program, and the second part runs it.
If you’re more comfortable using a phone, you can search for “C++ Compiler” on Google Play for Android, or use the following online compilers:
Although these websites can also be used on a computer, we recommend following the first steps of this article and installing the necessary tools on your own computer.
All our application needs to do is display or print the message “Hello, World!” To achieve this, all we need is the following code:
cout << "Hello, World!"This line of code is quite understandable. It simply sends the “Hello, World!” message to the output using cout.
One might wonder, what does the << sign represent? In a way, it can be interpreted as an arrow indicating the data flow or the direction in which the data is moving. In this case, we want to send our message to the output, so the arrow points to cout.
The other question that may arise is why we wrap our message in double quotations("). Well, every text that you want to include in your program that is not meant to be processed or understood by the computer needs to be enclosed in double quotations. In our example, we want the computer to understand cout and execute it, but we don’t want the computer to process what “Hello, World!” means (as it’s clearly not a command understood by computers). Instead, we want the computer to keep our message as-is to pass it later to cout.
Unfortunately, this code is not going to run and has a few problems that we need to fix.
The first issue is that the language requires you to end each line of code, or each “expression”, command, statement or thought, or whatever term you prefer, with a semi-colon (
;). So, we need to add that:
cout << "Hello, World!";The next issue is that in the execution of your code will start from the “main” function. In fact, it is the “main” section of your code that is executed when you run the program, and so if you don’t declare “main” your program won’t know where its start point is and wouldn’t be able to run.
But what’s a function?
We’ll cover functions in future projects, but for now, just know that in order for your code to compile and run, you need to put it inside the “main” section in the template below:
int main() {
}So, putting the code in our little template it would look like this:
int main() {
cout << "Hello, World!";
}The other thing is that cout is not inherently a part of the language itself but rather a component of it’s standard library.
A library consists of code that other programmers have written and shared to simplify your development process.
The standard library, in particular, is a collection of these helper utilities shipped with the language to provide fundamental tools necessary for creating applications.
When you use a library, you need to tell the compiler to include that library in your program. cout belongs to the iostream(Input/Output Stream) library, which in this example we are only utilizing its output stream. So including the iostream library, our code will look like this:
#include <iostream>
int main() {
cout << "Hello, World!";
}Well, our program is almost complete. Just one final adjustment before we can get it to run. As mentioned before, cout belongs to the standard library, which places all its definitions in a namespace called std. In order to instruct the compiler to search for cout in that namespace, we have two options.
First, we can specify it directly when using cout like this:
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}Alternatively, we can tell the compiler at the beginning of our program that we want it to check that namespace every time it needs to find a name like cout. While this second option has some downsides, for now, it simplifies our process, so we’ll continue with it:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}Namespaces are a feature in that allow us to organize our code by placing definitions into separate named spaces, hence the name.
This helps us structure our code more effectively and prevents naming conflicts, which is exactly why standard library’s definitions are in a certain namespace. However, it’s a concept we’ll cover in detail later on, so don’t worry about it for now.
As of this point, our program runs without any errors, but there’s still a minor issue fixing which requires a little back story.
Early computers didn’t have the power to produce graphics like ours do now. Consequently, to operate them, you had to use commands executed in a text-based environment, known as the Command Line Interface (CLI). The CLI used a concept called a prompt to ask the user to enter the next command.

How is all of that related to our application? Well, our program is also a “CLI” application, and even though it correctly prints “Hello, World!” it prints it on the same line as the prompt, resembling this, which is not an ideal output:
Hello, Worldbash $Most programs add an Enter or Return character at the end of their output. This character, which in is represented by
"\n", creates a new line, just like when you press the Enter or Return key on your keyboard. By adding "\n" to the end of our output, we ensure that the prompt appears on the next line, making it easier to read and follow.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!\n";
}This code is perfectly fine but adding “\n” may not be the preferred way of printing a new line in (less common, although it’s still completely valid). The somewhat more common approach is to use
endl (end of line), which is declared in the standard library, to move the cursor to the next line.
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
}Notice that in this example, cout is taking in two inputs: the text “Hello, World!” and endl.
What’s this code snippet’s output?
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl << "Hey, Big Blue Marble!" << endl;
}Hello, World!Hey, Big Blue Marble!
Hello, World!
Hey, Big Blue Marble!
Hello, World!
Hey, Big Blue Marble!
Correct
The cout statement can indeed include multiple parameters to be printed. So the code prints “Hello, World!” goes to the next line (which is done by the first endl) and then prints Hey, Big Blue Marble!
Eventually it goes to the next line to avoid the issue with the command line prompt.
In this project, we wrote our first-ever program. Below is its output:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
}Hello, World!