Editing, Compiling and Executing a Simple Program

A simple C++ program to add two numbers

The following is an example of a simple program (source code) written in the C++ programming language. The program is short but nevertheless complete. The program is designed to read two numbers typed by a user at the keyboard; compute their sum and display the result on the screen.

// Program to add two integers typed by user at keyboard
#include <iostream>
using namespace std;

int main()
{
   int a, b, total;
           
   cout << "Enter integers to be added:" << endl;
   cin >> a >> b;
   total = a + b;
   cout << "The sum is " << total << endl;

   return 0;
}

Overview of program structure and syntax

C++ uses notation that may appear strange to non-programmers. The notation is part of the syntax of a programming language, i.e. the formal rules that specify the structure of a legal program. The notation and explanations which follow will appear strange if you have never written a computer program. Do not worry about them or how the program works. This will be explained in more detail later, in section 3, 4 and 5. The following is an overview.

Every C++ program consists of a header and a main body and has the following structure.

// Comment statements which are ignored by computer but inform reader
#include < header file name>
    
int main()
{
    declaration of variables;
    statements;

    return 0;
}

We will consider each line of the program: for convenience the program is reproduced below with line numbers to allow us to comment on details of the program. You must not put line numbers in an actual program.

1  // Program to add two integers typed by user at keyboard
2  #include <iostream>
3  using namespace std;
4
5  int main()
6  {
7     int a, b, total;
8      
9     cout << "Enter integers to be added:" << endl;
10    cin >> a >> b;
11    total = a + b;
12    cout << "The sum is " << total << endl;
13 
14    return 0;
15 }

Line 1

At the top of the program are comments and instructions which will not be executed. Lines beginning with // indicate that the rest of the line is a comment. Comments are inserted by programmers to help people read and understand the program. Here the comment states the purpose of the program. Comments are not executed by the computer. They can in general be placed anywhere in a program.

Line 2

Lines beginning with # are instructions to the compiler's preprocessor. The include instruction says "what follows is a file name, find that file and insert its contents right here". It is used to include the contents of a file of definitions which will be used in the program. Here the file iostream contains the definitions of some of the symbols used later in the program (e.g. cin, cout).

Line 3

This is an advanced feature of C++. It is used to specify that names used in the program (such as cin and cout) are defined in the standard C and C++ libraries. This is used to avoid problems with other libraries which may also use these names.

Line 5

When the program is executed the instructions will be executed in the order they appear in the main body of the program. The main body is delimited by main() and the opening and closing braces (curly brackets). This line also specifies that main() will return a value of type integer (int) on its completion (see line 14). Every C++ program, irrespective of what it is computing, begins in this way.

Line 6

The opening (left) brace marks the beginning of the main body of the program. The main body consists of instructions which are declarations defining the data or statements on how the data should be processed. All C++ declarations and statements must end with a semicolon.

Line 7

This is a declaration. The words a, b and total are the names of variables. A variable is a location in the computer's memory where a value can be stored for use by a program. We can assign and refer to values stored at these locations by simply using the variable's name. The declaration also specifies the variable type. Here the variables a, b and total are declared to be data of type int which means these variables hold integer values. At this stage, the values of the variables are undefined.

Line 9

This statement instructs the computer to output the string of characters contained between the quotation marks, followed by a new line (endl). The location of the output is denoted by cout which in this case will be the terminal screen.

Line 10

This statement instructs the computer to read data typed in at the keyboard (standard input), denoted by cin. These values are assigned to (stored in) variables a and b.

Line 11

This statement is an arithmetic expression which assigns the value of the expression a + b (the sum of the integer values stored at a and b) to the variable total.

Line 12

Instructs the computer to display the value of the variable total.

Line 14

The last instruction of every program is the return statement. The return statement with the integer value 0 (zero) is used to indicate to the operating system that the program has terminated successfully.

Line 15

The closing (right) brace marks the end of the main body of the program.

Blank lines

(Lines 4, 8 and 13) have been introduced to make the program more readable. They will be ignored by the compiler. Whitespace (spaces, tabs and newlines) are also ignored (unless they are part of a string of characters contained between quotation marks). They too can be used to enhance the visual appearance of a program.

Indentation

It does not matter where you place statements, either on the same line or on separate lines. A common and accepted style is that you indent after each opening brace and move back at each closing brace.

The development environment and the development cycle

C++ programs go through 3 main phases during development: editing (writing the program), compiling (i.e. translating the program to executable code and detecting syntax errors) and running the program and checking for logical errors (called debugging).

  1. Edit
    The first phase consists of editing a file by typing in the C++ program with a text editor and making corrections if necessary. The program is stored as a text file on the disk, usually with the file extension .cc to indicate that it is a C++ program (e.g. SimpleAdder.cc ).
  2. Compile
    A compiler translates the C++ program into machine language code (object code) which it stores on the disk as a file with the extension .o ( e.g. SimpleAdder.o). A linker then links the object code with standard library routines that the program may use and creates an executable image which is also saved on disk, usually as a file with the file name without any extension (e.g. SimpleAdder).
  3. Execute
    The executable is loaded from the disk to memory and the computer's processing unit (Central Processing Unit) executes the program one instruction at a time.
Back to top