next up previous
Next: About this document ... Up: A first C++ program: Previous: What is Compiling and

Explanation of the Program

Here is a simplified line-by-line explanation of the program hello.cc.




  #include <iostream> 
  using namespace std;

Most of your programs in this course will have these lines at or near the top. For now, suffice it to say, they are needed to tell the compiler about cout. The iostream (sometimes you will see iostream.h) is the name of a file called a ``header file'' (sometimes called an ``include file''). This header file is standard and comes with the compiler, so you don't need to be concerned about where, exactly, it is found in the file system. It is a fragment of C++ code. The #include directive causes this standard fragment to be inserted in your code at that location.


  int main()                       
  {
  ...
  }

All executables must have a program module called ``main'' where execution begins. This statement specifies that all the lines between the open and close curly brace are part of the main program module. In this example the main module consists of only one statement.

The main module is regarded by the compiler as a function subprogram. We will discuss the type declaration int and the way functions work later in this course. For now, simply use this pattern for all of your main programs.




    cout << "Hello, world.\n";

The statement causes the character string Hello, world. to be written to the console. This is a simple C++ statement. All simple statements must end with a semicolon ;. The special character \n means ``end of line'' and breaks the line on your console, but is otherwise invisible. The double quotes are used to specify the beginning and ending of the character string. The name cout specifies the console screen (i.e. the standard output device) and the << is an operator that works with cout to cause what comes next to be sent to the console. As a mnemonic, think of the operator as an arrow that specifies that information flows to the console.


next up previous
Next: About this document ... Up: A first C++ program: Previous: What is Compiling and
Carleton DeTar 2007-08-17