next up previous contents
Next: Doing Things With Objects Up: Getting Started in C++ Previous: Introduction   Contents

Subsections

Hello World!

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello World!\n";
}

This is the classic first program you write in a programming language, but especially in C or C++. It is called ``Hello World'' because it prints the text Hello World! on the screen and exits.

Please notice that C++ is a case-sensitive language. In other words, main and Main are not the same things. Do not forget that when you want to try something out.

But now let's analyze it line by line. The first line is

#include <iostream>
and it is a preprocessor directive. (All lines starting by # are preprocessor directives). It is not a ``real'' C++ command, just a command to the compiler telling it to take the file iostream and to insert it instead of the directive. This is necessary because there the file iostream introduces some new commands we need later.

The second line

using namespace std;
tells the compiler that we will be using things which are in the namespace std. A namespace is a bit like a directory with files in. We tell the compiler that we will be using these files without every time specifying the namespace. This is necessary because the things in the file iostream are in the namespace std. Don't worry if you do not understand this now.

The third line

int main()
declares the main function (it is called a ``declarator''). This is necessary in every C++ program, as execution starts in the main function. I'll explain that better later on. For the time being just remember that every C++ program has a main function.

The fourth line

{
marks the beginning of the body of the function main.

The fifth line

cout << "Hello World!\n";
is the only statement inside the function body. It prints Hello World! on the screen. To do that, it uses cout, which in C++ means standard output (generally the screen) and sends it the string it wants to print. We had to include iostream before exactly because of this: cout is one of the things defined in that file. If we hadn't used using namespace std; we'd have to write std::cout instead.

You might wonder what the \n at the end of the string means; it is a newline character, a character which makes a new line start. This way the output under Unix might look something like this:

$ ./helloworld
Hello World!
$
while without the \n it would be something like this:
$ ./helloworld
Hello World!$
Of course, the same thing is true for Windows as well, with the difference that your prompt will be C:\> or something like that.

Finally, the line

}
marks the end of the body of the main function.

Do not worry if you do not fully understand everything right now; this was just a quick overview of how a simple program works. We'll dive into the details later.

The Minimal C++ Program

The minimal (shortest possible) C++ program is the following:

int main(){}
As you can see, it only contains the definition of the main function with an empty body. In fact that is enough for a perfectly valid C++ program; it does not need to include any other files because it does not need any functionality.

Of course, the minimal program does nothing.


next up previous contents
Next: Doing Things With Objects Up: Getting Started in C++ Previous: Introduction   Contents
Aaron Isotton <aaron@isotton.com>
2003-02-24