Chapter 4 Hello World!

 

4.1  Getting Started

 

Writing your first program in any new programming language can be challenging.  Often, the first program many people write simply prints the following words to the screen: "Hello World!".  The Hello World program is not about learning programming.  It's about how to use the compiler to write your source code, compile the source code, and run the compiler program.

4.2  Your First Program Using Code::Blocks

 

In Code::Blocks do the following steps:

1.  File Menu -> New -> Empty File
2.  Type the source code shown in the screen print below
3.  Click on Build and Run (circled in red)
 


 

If you have no syntax errors, then the output of your program will open in the command prompt window below.


 

4.3  Hello World Program Examined

This section will explain each line of the Hello World program.  If you are new to programming or C++, do not panic if this program looks complicated and foreign.  The following program listing shows the basic Hello World program again.

Program 4.3 Hello World Program

#include <iostream>

using namespace std;

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

Output
Hello World!

The following table explains each line of this program.  Do not panic if it is not all clear for most people understanding will come later after lots of practice.

Hello World Program Examined

C++ Statement Explanation
#include <iostream> At the top of C++ programs, you will usually find include statements.  The C++ programming language is limited, so it is common to include libraries to add additional capabilities.  In this example, we have included the iostream library which provides us with various input/output commands including the cout statement that allows us to output text to the screen.
using namespace std; The standard library is a collection of functions, constants, and classes that extends the C++ language providing additional functionality.  This line says to treat the identifiers (function, constant, class names) in the standard library as though they are part of the main program.  This line should be used in almost every program after your include statements.
int main ( ) All C++ code is located in containers called functions.  All programs will have a function named main which executes first when you run the program. Functions usually return a variable.  It is customary to have the main function return an int.
{ The open brace begins the function (container) which holds the program.  It will always be located immediately after a function header i.e. main().
  cout << "Hello World!"; This is really the first and only line of code in our program.  The cout (console out) statement tells the program to output text to the screen.  The text to be output is contained within double quotes.  Most C++ statements are terminated with a semicolon.
  return 0; Since the main function returns an int variable, the last line in this function should return 0.  This means that the program exited normally.
} The close brace terminates the function containing the code.