Introduction

Here is a traditional first C++ program. You can get a copy of this program and test it if you like. A good programmer knows precisely what each part of a program means to the computer. This page will take this program, piece by piece, and explain how the compiler parses it.
//Purpose: To show the rules of C++ programming
#include <iostream.h>
int main()
{  
   cout << "Hello, World!\n";

   return 0;
}  // main

The Example Parsed

Because this program has been typed in tidily we can take each line in turn. Click a line below to see how the compiler parses it, then use the left-arrow or back button to come back to here:
//Purpose: To show the rules of C++ programming
#include <iostream.h>

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

   return 0;
}  // main

Line by Line


The first line in the program is a comment:
//Purpose: To show the rules of C++ programming
This kind of comment stats with two slashes(//) and ends at the end of the line. The compiler ignores comments. A programmer writes them to communicate with other programmers some important information that does not effect the running of the program. This is information about the Who, When, and Why of the program. This comment says why it is the way it is.
The second line of the program:
#include <iostream.h>
is a directive. This directive instructs the compiler to find a file called iostream.h and put read it as if it had been written here in this file. The Iostrem library file defines the rules that let C++ program input and output streams of data. IO is short for Input/Output.

The compiler parses the line into these parts:

  1. #: Indicates a directive
  2. include: Indicates that a file is to be read here.
  3. <: Indicates that the file is in the C++ library. It is called a open broken bracket, or brocket.
  4. iostream.h: is the name of the file as interpretted by the operating system.
  5. >: Indicates that the file is in the C++ library. It is called a closing brocket.

The following line:
int main()
introduces the main function of the program. This is where the computer will start to run the program.

It is a function header. It says that this is the main part of the program and that when completed it will exit and return an integer value to the operating system that started the program running. This integer will say whether the program was successful or if it failed to achieve its purpose.

The parentheses () indiate that this function (the whole program) has no arguments. This means that the user who runs the program can not supply any options to the program. So this is program will be compiled into a simple one-word command that can be typed, and will do something.


The program is made up of a sequence of statements between an open brace and a close brace. So this line:
{  
Indicates the satrt of the statements that the program will excute when run. Each open brace { is matched up with a closing brace } by the compiler, or else the program has a syntax error.
This line is the first statement in the porgram:
   cout << "Hello, World!\n";
This is the first command that the program will obey because it follows imediately after the main(){. It is a special type of command or statement that is called an expression_statement. An expression ststement has an expression and a semicolon ( ; ) at the end:
  1. the expression:cout << "Hello, World!\n";
  2. the semicolon: ;
The expression is an infix expression. It therefore has three parts:
  1. left_hand_operand: cout
  2. operator: <<
  3. right_hand_operand: "Hello, World!\n";
The first part of the statement cout is a variable defined in <iostream.h>. It is the name of the place where C output is placed. This normally your screen. The second part is the operator << . This is also defined in <iostream.h> and has the effect of outputting the value of the third argument onto the end of the first argument.

The third part of the statement is a string:
"Hello, World!\n"; The operator << outputs each of these characters in turn until it gets to the special terminating character.


"Hello, World!\n";

A string starts with a quotation marke ". It ends with the same symbol. It represents a series of characters stored in the computer's memory. These characters do not include the quotation marks! They do have a special character called the null character. written '\0' after the last one:
The next line in the program is blank. This is very useful for making the program easier to read and understand. However the compiler does nothing with blank lines and ignores them.
The following line in this program:
   return 0;
is called a "return statement" for obvious reasons. It has three parts:
  1. The reserved word: return
  2. an expression
  3. a semicolon ; Its effect is to evaluate the expression and store the value. The computer goes back to the next thing to be done after the fucntion call. In this case the expression is a special constant 0 that indicates to the operating system that the program has done its work correctly. Any other value would signal that somethin unexpected happened.

    In this program the return statement means: stop this program running and indicate that it had zero errors.


The end of the program:
}  // main
The above line has two parts. The right brace ('}') matches the open brace '{' where the program started. It therefore makes the end of the program. It tells the compiler that the 'main()' function is now complete. There is a comment after the brace '}' that the coder added so that he or she knows precisely what is finishing. The compiler ignores comments like this. They help programmers understand what is going on.

Definitions

'\n'

This symbol is typed into the program as two characters - a back slash(\) and a letter 'n'. It is stored inside the computer as a single special character. It indicates achange to a new line. Put '\n' at the end of each line of output text! You can also indicate end of line with a special output manipulator written 'endl' in C++.

'\0'

This is the null character. It is shown as two characters but it is stored in the computers memory as a single character with value zero. It is used to terminate strings in C and C++.

Compiler

A program that translate the code you write ( the source code) into a language that a computer can run.

Parse

Before a a piece of text can be understood by someone it must be split up into pieces. We usually do this so quickly and easily that we are not aware of the process. The main task of a C++ compiler is split up the given text into pieces according to the rules of C++. These rules are called the syntax of C++.

Syntax

The syntax of a programming languages defines what is legal and also how to split the input text into pieces ready for translations.

For example

In your textbook you will find these rules are spelled out for every part of the language that you use. It is important to be able to recall these a precisely as possible. It is also importatn to be able to write text that fits these rules, and to be able to spot when a piece of text does not follow the rules (a syntax error). You need to be able to scan a program and make fit the syntax of C++, as fast and accurately as possible.

Proffessional programmers also master the official syntax of their langusges. See The ANSI/ISO standards and my own description of C++ A comment is either a block comment starting with /* snd ending with <*/> or an end-of-line comment that starts with // and ends at the end of the line.

Directive

A directive is a linethatdirects the compiler to do something special. They all start with a hash mark (#) and fill up a single line of the program.

Header

A Function Header introduces a new function and defines three things: the type of data (if any) that it returns when called, its name (as used in a call), and its arguments (if any are required) in parentheses.

Function

A Function is a named piece of code that can be called from other parts of the program in which it is declared. A function call means that the computer jumps to the start of the function, executes the statements in its body and then jumps back to the statement after the function call.

Declaration

A declaration in C++ introduces a new name and gives it a meaning for some part of the program.

Infix

An infix expression is a kind of expression where an infix operator is placed inbetween two other expressions.

Operand

An operand is something that can be operated on by and operator. In C++ operands are nealry always some kind of expressions.

Operator

An operator is a symbol indicating an operation. C++ has lots of operators including:

expressions

An expression indicates a computation that returns a values of some type or other. This value can be stored, or used as part of another expression, or it can be returned as the result of a function, but it can also be ignored. The simplest expressions are constants like 1.2345 and "1bc" . The next mostcomplex expression is a variable. Variables are the names of pieces of memory in the computer. Their values are the data stored in that place. They vary because the vlaue in their piece of memory can change. Expressions are constructed out of simpler expressions stuck togther with operators and function calls