Hello, World!
1.2 The classic first program
Here is a version of the classic first program. It writes “Hello, World!” on your screen:
Click here to view code image
// This program outputs the message "Hello, World!" to the monitor import std; // gain access to the C++ standard library
int main() // C++ programs start by executing the function main {
std::cout << "Hello, World!\n"; // output "Hello, World!"
return 0;
}
Think of this text as a set of instructions that we give to the computer to execute, much as we would give a recipe to a cook to follow, or as a list of assembly instructions for us to follow to get a new toy working. Let’s discuss what each line of this program does, starting with the line
Click here to view code image
std::cout << "Hello, World!\n"; // output "Hello, World!"
CC
That’s the line that actually produces the output. It prints the characters Hello, World! followed by a newline; that is, after writing Hello, World!, the cursor will be placed at the start of the next line. A cursor is a little blinking character or line showing where you can type the next character.
In C++, string literals are delimited by double quotes ("); that is, "Hello, World!\n" is a string of characters. The \n is a special character indicating a newline. The name cout refers to a standard output stream. Characters “put into cout” using the output operator << will appear on the screen. The name
cout is pronounced “see-out” and is an abbreviation of “character output stream.” You’ll find abbreviations rather common in programming.
Naturally, an abbreviation can be a bit of a nuisance the first time you see it and have to remember it, but once you start using abbreviations repeatedly, they become second nature, and they are essential for keeping program text short and manageable.
The std:: in std::cout says that the cout is to be found in the standard library that we made accessible with import std;
The end of that line
// output "Hello, World!"
is a comment. Anything written after the token // (that’s the character /, called
“slash,” twice) on a line is a comment. Comments are ignored by the
compiler and written for the benefit of programmers who read the code. Here, we used the comment to tell you what the beginning of that line actually did.
Comments are written to describe what the program is intended to do and in general to provide information useful for humans that can’t be directly expressed in code. The person most likely to benefit from the comments in your code is you – when you come back to that code next week, or next year, and have forgotten exactly why you wrote the code the way you did. So, document your programs well. In §4.7.2.1 and §6.6.4, we’ll discuss what makes good comments.
CC
A program is written for two audiences. Naturally, we write code for computers to execute. However, we spend long hours reading and modifying the code. Thus, programmers are another audience for programs. So, writing
code is also a form of human-to-human communication. In fact, it makes sense to consider the human readers of our code our primary audience: if they (we) don’t find the code reasonably easy to understand, the code is unlikely to ever become correct. So, please don’t forget: code is for reading – do all you can to make it readable.
The first line of the program is a typical comment; it simply tells the human reader what the program is supposed to do:
Click here to view code image
// This program outputs the message "Hello, World!" to the monitor
Such comments are useful because the code itself says what the program does, not what we meant it to do. Also, we can usually explain (roughly) what a program should do to a human much more concisely than we can express it (in detail) in code to a computer. Often such a comment is the first part of the program we write. If nothing else, it reminds us what we are trying to do.
The next line
import std;
is a module import statement. It instructs the computer to make available (“to import”) facilities from a module called std. This is a standard module
making all facilities from the C++ standard library available. We will explain its contents as we go along. For this program, the importance of std is that we make the standard C++ stream I/O facilities available. Here, we just use the standard output stream, cout, and its output operator, <<.
How does a computer know where to start executing a program? It looks for a function called main and starts executing the instructions it finds there.
Here is the function main of our “Hello, World!” program:
Click here to view code image
int main() // C++ programs start by executing the function main {
std::cout << "Hello, World!\n"; // output "Hello, World!"
return 0;
}
CC
Every C++ program must have a function called main to tell it where to start executing. A function is basically a named sequence of instructions for the computer to execute in the order in which they are written. A function has four parts:
A return type, here int (meaning “integer”), which specifies what kind of result, if any, the function will return to whoever asked for it to be
executed. The word int is a reserved word in C++ (a keyword), so int
cannot be used as the name of anything else.
A name, here main.
A parameter list enclosed in parentheses (see §7.2 and §7.4), here (); in this case, the parameter list is empty.
A function body enclosed in a set of “curly braces,” { }, which lists the actions (called statements) that the function is to perform.
It follows that the minimal C++ program is simply
int main() { }
That’s not of much use, though, because it doesn’t do anything. The main()
(“the main function”) of our “Hello, World!” program has two statements in its body:
Click here to view code image
std::cout << "Hello, World!\n"; // output "Hello, World!"
return 0;
First it’ll write Hello, World! to the screen, and then it will return a value 0
(zero) to whoever called it. Since main() is called by “the system,” we won’t use that return value. However, on some systems (notably Unix/Linux) it can be used to check whether the program succeeded. A zero (0) returned by
main() indicates that the program terminated successfully.
A part of a C++ program that specifies an action is called a statement.