Hello World!
Learn the basic syntax of C++ in detail.
Let’s revisit the “Hello World!” program from our previous lesson. We’ll break it down and discuss each line in detail, step by step!
//This program displays “Hello World!” on the screen#include <iostream>using namespace std;int main() {cout << "Hello World!";return 0;}
Feel free to experiment with the code in the above widget.
Adding comments
C++ provides its users with the ability to write pointers in the code in the form of comments. Comments serve as a helper to coders so they can understand the code once they revisit the code. To add a comment in the code, add //
double frontslash before the comment. In the code, we have:
//This program displays “Hello World!” on the screen
You can also add a multi-line comment in the code using /* */
and add the comment between the lines. You can test multi-line comments here:
#include <iostream>using namespace std;// Beginning of our main functionint main() {/*- This code is used to print Hello World!- We use the cout statement for that*/cout << "Hello World!";return 0;}
The #include
statement
It is written as:
#include <iostream>
The hash sign (#
) signifies the start of a #include
command is a specific preprocessor command that effectively copies and pastes the entire text of the file specified between the angle brackets into the source code. In this case, the file is iostream
which is a standard file that should come with the C++ compiler. This file name is short for “input-output streams”; in short, it contains code for displaying and getting the text from the user. The “include” mechanism, however, can be used both for standard code provided by the compiler and for reusable files created by the programmer.
using namespace std
It is written as:
using namespace std;
C++ supports the concept of namespaces. A namespace is essentially a prefix that is applied to all the names in a certain set. One way to think about namespaces is that they are like toolboxes with different useful tools. The using command tells the compiler to allow all the names in the std
namespace to be usable without their prefix. The iostream
file defines one name used in this program—cout
—which is defined in the std namespace. std
is short for “standard” since these names are defined in the
Without using the std namespace, the name would have to include the prefix and be written as std::cout
. If we continue with the toolbox example, this code would be saying, “Use the cout tools from the std toolbox.”
Note: You should either remember the fact that the
iostream
file uses thestd
namespace or look it up in the documentation for theiostream
file because C++ does not make this connection for you explicitly.
A slight feeling of annoyance at having to type this connection every time you write a new C++ program is entirely normal—indeed, justified. However, it can be seen as a small price to pay for avoiding the tedious work of constantly retyping std::
in front of various terms in the program.
#include <iostream>int main() {std::cout << "Hello World!";return 0;}
In the above example, we removed namespace std
and now whenever we use std
names like cout
we have to add a prefix to access it—std::cout
.
int main()
int main()
is written as:
int main() {
This line is the entry point of all C++ programs. When the program execution starts, the operating system first executes this line, followed by the first line of code that comes after the opening curly brace {
. The terms int
and ()
hold a special meaning. We’ll discuss these in detail in a later lesson. For now, knowing that this line is the entry point for the program should suffice. By execution, we mean performing the actions specified by the statements in your program.
int main()
is a function. A function is a block of code that executes to achieve a certain objective. We will cover functions in more detail later.
The cout
command
Let's look at the code below:
#include <iostream>using namespace std;int main() {cout << "Hello World!";return 0;}
The name cout
is short for “character output.” cout
, together with <<
, is used to display whatever comes after it on the screen. In this case, Hello World!
will be displayed. Note that the quotation mark will not be displayed on the screen. They are there just to mark the starting point and the ending point of the text and tell the compiler which characters need to be displayed. We will discuss these in more detail later in the course.
The { }
braces
The lines of code between {
and }
are referred to as a block of code. This means that these lines of code are related and would be executed together, line by line. {
signifies the start of a block of code, and }
signifies its end.
Semicolons
In programming, a statement is a unit of code that expresses some action that needs to be performed. cout << "Hello World!"
is a statement. Statements in C++ must be terminated with a semicolon. There are some exceptions that we will discuss as they come along in the course. For now, you can see that the preprocessor directive in our code #include <iostream>
doesn’t have a semicolon at the end. Similarly, the curly braces {
and }
, and the int main()
do not have a semicolon at the end.
Just like in the English language, where sentences are terminated with a period and, therefore, can span several lines—in C++ too, we can use as many spaces and new lines between the words of a C++ program as you wish to beautify your code, just as spaces are used to justify the text printed on the pages of a book.
The return
statement
The return
statement is written as:
return 0;
Executing the return
statement in the main returns the execution control to the operating system component that launched this program, in effect, terminating the execution of this program. By specifying the value 0
after return
, we are able to send the value 0
to the operating system component that had launched this program. This acts as a signal, informing the operating system that the program was able to run and terminate successfully. There are other uses of the return statement that will be covered in more detail later in the course.