In this introductory lesson, we'll create our first program in C++! We will start very simply. We'll just be outputting some text to the screen.
Things will ramp up quickly, but first, our priority is to make sure we have everything we need to start writing and running C++ programs.
If you do not have a development environment set up yet, let's start by going through some options we have.
If you do not yet have a code editor, now is the time to get one! There are plenty of free options for every major operating system.
For C++, I'd recommend these:
JetBrains also offers editors for C++ that work across all popular operating systems. Their products include CLion for general-purpose C++, as well as Rider which is tailored towards game development specifically.
Unlike the above three, JetBrains editors are not free. However, they have free education licenses if you're a student, and a free trial if you're not.
You will often hear the term "IDE" being used to describe the tools we use to build software. All the previous recommendations are IDEs.
In the past, developers needed to use a combination of tools to build software. In modern times, most developers now work in an IDE. These applications combine the most important tools into a single, unified user interface.
The screenshots we show in this course will be from Visual Studio 2022, but it's not that important what software you use. The interface and processes will be very similar, and the code will be entirely identical.
When installing your editor, ensure that you include C++ components during the setup if given the option. This can be done by reviewing the online documentation for your editor.
This course requires nothing specific or unusual - the basic ability to create modern C++ programs will fulfill our needs.
In Visual Studio, this can be done by selecting one of the C++ Workloads during installation - for example, Desktop Development with C++ or Game Development with C++
If you already have Visual Studio installed without these components, you can configure your installation simply by running the installer again.
Once we've got your editor of choice set up, create a new, blank C++ project with it.
In Visual Studio, this involves creating an Empty Project from the Create a new project window. This window should be accessible from your start screen, or File > New > Project from the top menu bar.
Once we create the project, we can then add our first C++ file to it.
Other editors may have automatically included a starting file when they generated the project, in which case this step can be skipped.
In Visual Studio, we can add a file from the top menu bar under Project > Add New Item.
We can call our file anything we want - I've gone with main.cpp
In Visual Studio, this will create a new, empty file in our project.
If you're using a different editor, and that editor included a starting file for you, they may also have included some code in that file. If so, delete their starting code.
We want to have our starting code look exactly like what is below:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
}
To ensure everything works, paste this code into your main.cpp
, save your file then build and run the program.
In most IDEs, this can be done by pressing a button somewhere in the user interface. In Visual Studio, for example, we can use the Start Without Debugging button on the top menu.
Our program should open a terminal where we see the output of the code - a simple "Hello World!" message.
Hello World!
C++, and programming languages in general, are very strict about the symbols used in our code.
One of the most common reasons the simple previous example won’t work is that we’ve used the wrong form of quotation marks when entering "Hello World"
.
There are several different options we have. We must be specifically using the "
character, commonly called "straight double quotes". On most keyboards, this is either near the right-shift key or above the 2
on the top row.
Other options, such as ", ‘ and ` may look similar, but they are not equivalent, and won’t work.
Converting our code into a program that can be executed on a computer is usually referred to as compiling or building the program. The software that converts our code to an executable program is called a compiler.
When we install a C++ IDE like Visual Studio, a compiler is typically bundled into the package we downloaded. Microsoft’s C++ compiler is called MSVC (Microsoft Visual C++).
MSVC, and most C++ compilers, can be used as standalone programs - we can write code in any text editor and use a compiler to convert it to a program. IDEs like Visual Studio combine text editing and compilation into a single user interface to give us a more seamless experience.
By the end of this course, you'll understand what every line of code is doing, but for now, we need to skip over some things.
Until we introduce those more advanced topics, our code files should always follow the basic structure of the previous example.
Initially, we will limit our changes to just the area between the {
and }
Currently, that line of code is cout << "Hello World!";
.
cout
is an abbreviation of "character output". When we write (or "stream") things to the character output, it should appear in a terminal or console window when we run our program.
We stream things to the character output using <<
operator, with cout
on the left and the thing we want to send on the right.
An operator is a symbol that performs some action on one or more objects in our program. <<
is an example of an operator.
In the cout << "Hello World!"
example, the objects it is acting upon are cout
and "Hello World"
The objects an operator acts upon are sometimes called its operands. We’ll see many more examples of operators, and get very comfortable with them over the following lessons.
If the thing we want to send is some text, we need to put the text in double quotes, such as "Hello World!"
or "Goodbye!"
.
Finally, we need to end our statement using a semi-colon ;
In programming, a statement is an instruction - some action we are commanding the computer to carry out.
In C++, and many other programming languages, a semicolon ;
is used to denote the end of each statement.
We can stream as many things to the output as we want simply by repeating the pattern on additional lines:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "My Name is Ryan";
cout << "Goodbye";
}
Hello World!My Name is RyanGoodbye
Even though our previous code was spanned across multiple lines, we can see that our input wasn’t. As we’ll see later in this chapter, C++ is fairly flexible about the spacing around our code. There are many ways to lay out the code in any given file, and our layout choice generally won’t affect how our program behaves.
However, it would be nice if we could add some spacing that does affect the output of our program. To insert a line break into our output, we stream the special sequence "\n"
. Note the direction of the slash is important - we need \
, not /
Any subsequent output will then be on a new line:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << "\n";
cout << "My Name is Ryan";
cout << "\n";
cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye
We can also place the \n
character within an existing string of text - even in the middle of a string of text. It does not have to be streamed individually, so we could have written our previous program like this:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!\n";
cout << "My Name is Ryan\nGoodbye";
}
Hello World!
My Name is Ryan
Goodbye
endl
and std::endl
Another way of achieving line breaks, which we’ll see in other learning resources and examples, involves streaming endl
or std::endl
to the output. The following code shows an example of both:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
cout << endl;
cout << "My Name is Ryan";
cout << std::endl;
cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye
We’ll tend to prefer the \n
method, but we can consider either approach to be approximately equivalent.
Note however that the option of inserting the new line sequence in the middle of our text like "Hello\nWorld"
is not available when using endl
or std::endl
. Those need to be streamed in isolation.
<<
OperatorsWe can stream multiple things to the terminal in a single statement. This is done by using the <<
operator multiple times, sometimes referred to as chaining the operator:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << "\n";
cout << "My Name is Ryan" << "\n";
cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye
We can further shorten this program to a single statement. The following statement spans multiple lines to make our code easier to read, but it is still a single statement. We can tell when our statement ends by looking at where the semicolon is.
We’ve also added some additional space to make our code easier to follow, but this is optional:
#include <iostream>
using namespace std;
int main(){
cout << "Hello World!\n"
<< "My Name is Ryan\n"
<< "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye
We can output many different types of data. We will see more of this soon, but here's an example of outputting a number. Note, that we don't need to surround numbers with quotes:
#include <iostream>
using namespace std;
int main() {
cout << 15 << " is a number\n"
<< "Another number is " << 52;
}
15 is a number
Another number is 52
We should now have confirmed that our development environment is set up. We can write code, compile it, run it, and see our output.
In this introductory lesson, we have laid the groundwork for your journey into the world of coding. Here's a quick recap of what we've covered:
cout
for output\n
) and the concept of chaining <<
operators for streamlined code.In our next lesson, we will dive deeper into the core concepts of C++ programming. You'll learn about:
This upcoming lesson is crucial as it lays the foundation for more advanced topics in C++, setting the stage for creating more complex programs.
Getting our computer set up so we can create and build C++ programs. Then, creating our very first application
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way