C++ Programming/Examples/Hello world

Hello World - Writing, Compiling and Running a C++ Program
Below is an example of a simple C++ program:

When you write a program, you use a development environment. Your development environment can be a basic text editor or a feature rich C++ integrated development environment (IDE). You should not use a word processor like Microsoft Word, because it adds formatting codes to the text.

If a compiler is not already available to you, see the Where to get a compiler Section of the book.

Open your development environment and type the program shown (or copy and paste it) and save it as hello.cc.

Now compile it using the C++ compiler:

COMMAND_PROMPT> g++ hello.cc -o hello

The example uses GCC, the GNU Compiler Collection ( http://gcc.gnu.org ) but you could use any other compiler, or use an IDE to compile it. The above command would produce an executable called hello or hello.exe. Invoke the executable to run your first C++ program:

Unix: COMMAND_PROMPT> ./hello Hello World! COMMAND_PROMPT>

Microsoft Windows: COMMAND_PROMPT> dear hello Hello World! COMMAND_PROMPT>

Text that is italicized is typed by you and the bold text is output by the program. If you use an IDE, it might automatically color the code for you based on the syntax.

Troubleshooting
You don't have the GNU C++ compiler installed. If you have a different compiler, check its documentation for the correct compilation command.
 * g++: command not found

Lot of weird errors, mentioning many times:
 * Wrong Compiler Command

undefined reference to `std::basic_ostream' [..]

Usually ending with:

collect2: ld returned 1 exit status

To use g++ to compile your hello.cc, use:

g++ hello.cc -o hello

For gcc, use:

gcc hello.cc -o hello -lstdc++

You did not type the full path, try:
 * hello: command not found

./hello

Is there a hello program in this directory? Can you see it when you type ls? If not, your compilation (g++ hello.cc -o hello) failed or you have changed to a wrong directory.

If you do not specify -o hello, g++ names the output file a.out (assembler output) for historical reasons. In such a case, type:

./a.out

to execute the program.

The preprocessing directive
Some features of C++ are part of the language and some others are part of a standard library. The standard library is a body of code that is available with every C++ compiler that is standards compliant. When the C++ compiler compiles your program it usually also links it with the standard C++ library.

When you use features from the library, C++ requires you to declare the features you will be using. The first line in the program is a preprocessing directive. In our example it is shown bold and italicized:


 * The preprocessing Directive for IOStreams

This line causes the C++ declarations which are in the <tt>iostream</tt> header to be included for use in your program. Usually the compiler inserts the contents of a header file called <tt>iostream</tt> into the program. Where it puts it depends on the system. The location of such files may be described in your compiler's documentation. A list of standard C++ header files is in the standard headers reference tables.

The <tt>iostream</tt> header contains various declarations for input/output (I/O). It uses an abstraction of I/O mechanisms called streams. For example there is an output stream object called <tt>std::cout</tt> which is used to output text to the standard output. Usually, this displays the text on the computer screen.

The preprocessor is a part of the compiler which does some transformations to your code before the actual compiler sees it. For example, on encountering a <tt>#include </tt> directive, it replaces the directive with the contents of the <tt>iostream</tt> header file.

<tt>main</tt> Function
The lines above represent a block of C++ code, given the name <tt>main</tt>. Such a named block of code is called a function in C++ parlance. The contents of the block are called the body of the function.

The word <tt>int</tt> is shown in bold because it is a keyword. C++ keywords have some special meaning and are also reserved words, i.e., cannot be used for any purpose other than what they are meant for. On the other hand <tt>main</tt> is not a keyword and you can use it in many places where a keyword cannot be used (though that is not recommended, as confusion could result).

Every (standards-compliant) C++ program must define a function called <tt>main</tt>. This is where the execution of the program begins. As we shall see later, <tt>main</tt> may call other functions which may call yet other functions. The compiler arranges for <tt>main</tt> function to be called when the program begins executing. (Although this is generally true, it is not always true. There is an exception to <tt>main</tt>'s being executed at the very beginning that we will see later.)

Now let us look at the code inside the <tt>main</tt> function.

Printing Hello World!
The first line in <tt>main</tt> uses the <tt>std::cout</tt> object to print the string (sequence of characters) <tt>Hello World!</tt> and end the line:

std::cout << "Hello World!\n";

This line is a C++ statement. C++ statements are terminated by a semicolon (<tt>;</tt>). Within the statement <tt><<</tt>, called the insertion operator is used to output the string using the <tt>std::cout</tt> stream. C++ strings are enclosed within double quotes (<tt>"</tt>). The quotes themselves are not part of the string and hence not printed. The sequence <tt>\n</tt> is used within a string to indicate the end of the current line. Though the sequence is represented by two characters, it takes up only one character's worth of memory space. Hence the sequence <tt>\n</tt> is called the newline character. The actual procedure to start a new line is system-dependent but that is handled by the C++ standard library transparent to you.

Modifications to the Above Program
Here is the same program with minor modifications:

Comments
The line added at the beginning:

// This program just displays a string and exits

is a comment that tries to explain what the code does. Comments are essential to any non-trivial program so a person who is reading the code can understand what it is expected to do. There is no restriction to what is contained between the comment delimiters. The compiler just ignores all that is there in the comment. Comments are shown italicized in our examples. C++ supports two forms of comments:

/* This program displays a string   and then it exits */
 * Single line comments start with a <tt>//</tt> and extend up to the end of the line. These can also be used to the right of statements to explain what that statement does.
 * Multi-line comments start with a <tt>/*</tt> sequence and end with a <tt>*/</tt> sequence. These can be used for comments spanning multiple lines. These are also known as C-style comments as this was the only type of comment originally available in C. e.g.:

Comments are also used at times to enclose code that we temporarily want the compiler to ignore, but intend to use later. This is useful in debugging, the process of finding out bugs, or errors in the program. If a program does not give the intended result, by "commenting out" code, it might be possible to track which particular statement has a bug. As C-style comments can stop before the end of the line, these can be used to "comment out" a small portion of code within a line in the program.

Flushing the Output Stream Buffer
Whenever you write (i.e., send any output) to an output stream, it does not immediately get written. It is first stored in memory and may actually get written any time in the future. This process is called buffering and the regions in memory used for storing temporary data like this are called buffers. It is at times desirable to flush the output stream buffers to ensure all data has been written. This is achieved by applying the insertion operator to an output stream and the object <tt>std::endl</tt>. This is what is done by the line:

std::cout << std::endl;

Before flushing the buffer, <tt>std:endl</tt> also writes a newline character (which explains its name, end line). Hence the newline is omitted in the string printed in the previous line.

Returning Success Code
In most operating systems, every program is allowed to communicate to the invoker whether it finished execution successfully using a value called the exit status. As a convention, an exit status of 0 stands for success and any other value indicates failure. Different values for the exit status could be used to indicate different types of failures. In our simple program, we would like to exit with status 0.

C++ allows the <tt>main</tt> function to return an integer value, which is passed to the operating system as the exit status of the program. The statement:

return 0;

makes <tt>main</tt> to return the value 0. Since the <tt>main</tt> function is required to return an integer, the keyword <tt>int</tt> is used to begin the function definition. This statement is optional since the compiler automatically generates code to return 0 for the <tt>main</tt> function for the cases where control falls off without a <tt>return</tt> statement. This is why the first program worked without any return statements. Note that this is only a special case that applies only to the <tt>main</tt> function. For other functions you must return a value if they are declared to return anything.

Common Programming Error 1 Though the <tt>return</tt> statement is optional, <tt>main</tt> should not be declared to return <tt>void</tt> (a function declared as void is a function which does not return anything) as in some other languages like Java. Some C++ compilers may not complain about this, but it is wrong. Doing this could be equivalent to returning just about any random number that happened to be stored in a particular memory location or register, depending on the platform. This practice can also be potentially damaging to some operating systems, which rely on the return code to determine how to handle a crash or other abnormal exit.

Whitespace and Indentation
Spaces, tabs and newlines (line breaks) are usually called whitespace. These are ignored by the compiler except within quotes, apart from the rule that preprocessing directives and C++-style comments end at a newline. So the above program could as well be written as follows:

Note, however, that spaces are required to separate adjacent words and numbers. To make the program more readable, whitespace must be used appropriately.

The conventions followed when using whitespace to improve the readability of code constitute an Indent style. For example, with alternate indent styles, the program could be written like this:

or like this: