Skip to main content

Section 2.4 The Hello World Program

Traditionally, the first program you write when learning a new programming language is called the β€œHello World” program. All it does is output the words Hello, World! to the screen. In C++, that program looks like this:
Listing 2.4.1.
You can test out the program by pressing the "Run" button above the code. That will cause the code to be compiled and run on the server that hosts this book. The output will then be displayed below the code. The output should display the following:
Hello, World!
Let’s break down the Hello World program into its parts.
The first line of code is a preprocessor directive. Preprocessor directives are instructions to the C++ compiler that are executed before the program is compiled and start with #
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
Including a file in a C++ program essentially copies all the code from that file into the location of the #include. The iostream file contains the code that allows you to use to do input and output (io) in C++ programs.
Then there is a blank line:
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
Blank lines are ignored by the C++ compiler, but they make code easier to read by separating logical chunks of a program from each other. Here, the blank line separates the preprocessor directive from the rest of the program.
Next is the using namespace std; statement.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
A statement is a line of code that performs a basic action. In this case, the action is telling the compiler that we want it to assume that any names we use may actually start with std. This will allow us to shorten the full formal name std::cout to cout on line 7. This is a convenient way to reduce the amount we have to type, especially if we are going to use that name over and over. Alternatively, we could skip line 3 and write the full formal name every time.
Like most statements, it ends with a semicolon (; ). Semicolons function in C++ a little like periods do in English sentences - they indicate the end of a statement.
Line 5 defines a function named main.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
In C++ functions are used to group and organize sequences of statements. Every C++ program needs to have a function named main. When a C++ program runs, it starts at the first statement in main and ends when it finishes the last statement. Later, you will see programs that define more than one function. The word int indicates that this function returns an integer (a whole number) when it is finished. Don’t worry too much about these details right now, we will learn more about these parts of the program later. For now, just know that int main marks the start of the program.
Notice that line 5 does not end with a semicolon. It instead ends with a curly brace. (β€œcurly braces” are the name for { and }). C++ uses to group things together. In Hello.cpp, braces wrap the statements that are a part of the function definition. A good rule of thumb is that lines of code either are a complete and independent statement - and end in a semicolon - or they are the start of a group of related statements - in which case they end with a {.
Line 6 is a comment, which is a bit of English text that explains the code.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
When the C++ compiler sees //, it ignores everything from there until the end of the line. Comments have no effect on the execution of the program, but they make it easier for other programmers (and your future self) to understand what you meant to do.
Line 7 is a print statement that displays a message to the user.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
Printing in C++ is done with cout, which stands for "console output" - the console is a name for the screen where the input and output of a program are displayed. The << is an operator used to indicate that we are sending the data on the right to cout (the formal name for that symbol is the stream insertion operator). The data we are sending is the text "Hello, World!".
Phrases that appear in quotation marks are called strings, because they contain a sequence (a "string") of characters strung together in memory. Characters can be letters, numbers, punctuation marks, symbols, spaces, tabs, etc. The quotation marks indicate that the characters inside are text data, not computer instructions.
C++ is β€œcase-sensitive”, which means that uppercase and lowercase are not the same. For example, cout has to be in all lowercase letters - Cout and COUT won’t work.
As mentioned earlier, the statement return 0; on line 8 fulfills the promise made by int main to return a value.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}
Returning 0 from the main function lets the operating system know that the program finished without errors. Returning any other number would indicate some kind of issue. Using return 0 at the end of main is optional - the compiler will add it automatically if it is missing. You will see that many of the samples in this book rely on that trick.
Finally, line 9 is the end of the main function. The closing curly brace (}) matches the open curly brace on line 5 and indicates that the function is finished.
#include <iostream>

using namespace std;

int main() {
    // generate some simple output
    cout << "Hello, World!";
    return 0;
}

Checkpoint 2.4.1.

Checkpoint 2.4.2.

Multiple Response Which is true about writing a program?
  • The main marks the spot in the program where execution begins.
  • The main indicates where the program begins executing!
  • There is a limit the number of statements you can put in the main because they occupy system memory.
  • There is no limit to the number of statements you can put in the main, but it is good practice to keep it as short as possible.
  • Inside the main, program execution happens in order from top to bottom.
  • When the program runs, it starts by executing the first statement in main, and it continues until the last.
  • The main program is enclosed by parentheses.
  • The main program and all functions in C++ are enclosed by squiggly brackets ( { and } ).
  • The end of each statement is marked with a semicolon ( ; ).
  • Forgetting a semicolon will cause a compile error!

Checkpoint 2.4.3.

Checkpoint 2.4.4.

You have attempted of activities on this page.