C++ Programming/Examples/Hello world

From Wikibooks, open books for an open world
Jump to: navigation, search

Hello World - Writing, Compiling and Running a C++ Program[edit]

Below is an example of a simple C++ program:

// 'Hello World!' program 
 
#include <iostream>
 
int main()
{
  std::cout << "Hello World!" << std::endl;
  return 0;
}


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> 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[edit]

g++
command not found

You don't have the GNU C++ compiler installed. If you have a different compiler, check it's documentation for the correct compilation command.

Note:
There is detailed information on how to get a compiler and how to install it on the Where to get a compiler Section.
In Appendix B:External References you will also find references to other freely available compilers and even full IDEs you can use.

Wrong Compiler Command

Lot of weird errors, mentioning many times:

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++

Note:
For simplicity we assume that the file is in the path you are using...

hello
command not found

You did not type the full path, try:

./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.

Your First C++ Program Explained[edit]

The preprocessing directive[edit]

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
 #include <iostream>

This line causes the C++ declarations which are in the iostream header to be included for use in your program. Usually the compiler inserts the contents of a header file called iostream 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 iostream 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 std::cout 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 #include <iostream> directive, it replaces the directive with the contents of the iostream header file.

main Function[edit]

int main()
{
    // ...
}

The lines above represent a block of C++ code, given the name main. 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 int 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 main 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 main. This is where the execution of the program begins. As we shall see later, main may call other functions which may call yet other functions. The compiler arranges for main function to be called when the program begins executing. (Although this is generally true, it is not always true. There is an exception to main's being executed at the very beginning that we will see later.)

Now let us look at the code inside the main function.

Printing Hello World![edit]

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

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

This line is a C++ statement. C++ statements are terminated by a semicolon (;). Within the statement <<, called the insertion operator is used to output the string using the std::cout stream. C++ strings are enclosed within double quotes ("). The quotes themselves are not part of the string and hence not printed. The sequence \n 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 \n 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.

Note:
C Programmers should not confuse the insertion operator with the bitwise shift operator in C. C++ has a feature called operator overloading which allows the two interpretations of << to coexist. In C++, I/O is the primary use of << and >>, and bit shifting is a relatively uncommon use.

Modifications to the Above Program[edit]

Here is the same program with minor modifications:

// This program justs displays a string and exits
#include <iostream>
 
int main()
{
  std::cout << "Hello World!";
  std::cout << std::endl;
 
  return 0;
}

Comments[edit]

The line added at the beginning:

// This program justs 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:

  • Single line comments start with a // and extend upto 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 /* sequence and end with a */ sequence. These can be used for comments spanning multiple lines. These are also known as C-style comments as this is the only type of comments available in C. e.g.:
/* This program displays a string
   and then it exits */

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[edit]

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 std::endl. This is what is done by the line:

std::cout << std::endl;

Before flushing the buffer, std:endl 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[edit]

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 main 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 main to return the value 0. Since the main function is required to return an integer, the keyword int is used to begin the function definition. This statement is optional since the compiler automatically generates code to return 0 for the main function for the cases where control falls off without a return 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 main function. For other functions you must return a value if they are declared to return anything.

Common Programming Error 1 Though the return statement is optional, main should not be declared to return void (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[edit]

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:

// This program justs displays a string and exits, variation 1
#include <iostream>
 
int main() { std::cout<<"Hello World!"; std::cout<<std::endl; return 0; }

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:

// This program justs displays a string and exits, variation 2
#include <iostream>
 
int main() {
  std::cout << "Hello World!";
  std::cout << std::endl;
 
  return 0;
}

or like this:

// This program justs displays a string and exits
#include <iostream>
 
int main()
{
  std::cout << "Hello World!";
  std::cout << std::endl;
 
  return 0;
}