Previous Chapter Main Menu Next Section

THE STORY OF C++
A Day in the Life of a Programming Language

Chapter Two
The Basics of C++ and Program Development

Section I : Variables Section II: Programming Basics Section III: Iteration Section IV: Branch Statements

A. Memory and Variables
It's time to jump right in and play with C++. Almost all programs have some kind of input and some kind of output so let's start there. The notion of an input implies that some value is entered into the program from the 'outside' while an output implies that some value is sent out of the program. As a concrete example, we will work with a simple problem to find the sum of three numbers entered by a user. Of course, no one would ever write a computer program to do this - that is what calculators are for. But, we start with something simple to allow us to focus on C++ and not be distracted by the problem itself. Soon enough we shall focus on the issues related to developing a program for a complex problem.

It should be fairly obvious that there are three inputs here - the three numbers. It is probably obvious that there is one output - the sum. Note that the program could skip the output - the computer has no problem computing the sum of the three numbers and never telling us its answer! There is an important point here: the output is for us - we want to know the results of the computers work. And, as was noted above, if we do not tell the computer through the program exactly what we want it to do (in this case, to output the results), we won't get what we want.

So, now we have three input values and one output value. Such values have to somehow be stored inside the computer - inside the computer's memory. A modern computer has millions of memory locations each with a unique numeric address. Consider the figure below. This represents a picture of the beginning and end of a memory with 1,000,000 locations. The first location has the address 000000, the last location has the address 999,999. (Note that we count from 0 so the memory location with address 999,999 is really the millionth memory location.)

0  1 2  3  4  5  6  7  8  9 
00000                     
00001                     
00002                     
.
.
.
0  1 2  3  4  5  6  7  8  9 
99997                     
99998                     
99999                     
Remember machine language? Well, at that level the numeric addresses are actually used. But, that is not the way humans operate best. Imagine instructions such as:

Add the contents of address 0012345 to the contents of address 3477778 and store the result in address 5713459.

Imagine further that you use many other memory locations in your program and that later in your program you needed to use the results of this addition. You would have to remember that address 713459 holds the result and not addresses 477778 or 012345 or any of the other 999,997 possible addresses.

To make our work as programmers easier, higher level languages allow us to make up names such as 'value1', 'value2', and 'sum' to be used in our programs. These names are referred to as 'variables.' These variables will get translated later at compile time into the numeric memory addresses required by the computer.

It is very important that you understand what a computer variable is. Here is one way of putting it:

A variable in a programming language is a symbolic name for a computer memory location.

In other words, a variable in a program is a name (symbol) we as humans give to a memory location that will hold some piece of information useful in the program. As an example from everyday life, consider the house where one of the authors lives. There is a formal address for that house - 205-A Montezuma Rt., Las Vegas, NM. This address can be seen as equivalent to the formal, numeric address of a memory location. Someone can always give this address to someone else and, with a map, that someone else can find the house. But, for people who know the author, there is also a 'symbolic' address - "Sollohub's house." For people who know the author, "Sollohub's house" really refers to (is a symbolic name for) 205-A Montezuma Rt. etc.

B. Variable Types
There are different types of variables. Let's look at three of the types that are built into C++. First, there is the integer type. An integer is any positive or negative number that does not have a decimal point. So, the following are all integers:

 

3;    0;    45632;    -4;     -893;    245,564,789,100

The following numbers are NOT integers:

 

4.2;     3.567;    3.0;    2.345678

This leads us to a second variable type - the 'double'. Doubles are numbers with decimal points. Therefore, all the numbers in the second set of examples above are doubles.

Third, there is the character type. To start with, each letter of the alphabet and each punctuation mark is a character. Where it gets a bit complex is when the discussion turns to digits (0, 1,2,3,4,5,6,7,8,9) and the distinction between upper and lower case letters.

Note that '3' is an integer according to the example above but '3.0' is a double. Of course, '3' and '3.0' refer to the same value but to a computer they represent different types. That is because they are represented differently in the computer's memory.

This notion of representation is very important. Consider the value 5 (five objects). In the Western World we usually represent the value 5 with the symbol '5'. But, we also know that the Romans used the symbol 'V' to represent the value 5 while in the Arab world a heart represents the same value. Who knows what symbol Alpha Centaurians use! In other words, there is a difference between a value and the symbol or symbols used to represent that value. Given this, the '3' we referred to above can also be seen as a character - the character or symbol which we use to stand for (represent) the value 3.

Computers have their own representational systems for integers, doubles, and characters. Integers and doubles are usually represented with the binary number system. This will not be discussed in detail here but you should know that the representation for '3' stored as an integer is different from the representation for the same value stored as a double. And, both representations are different from the representation for the symbol (character) 3 . Likewise, there is a unique representation for the symbol (character) 'a' and a different, unique representation for the symbol (character) 'A'.

Computer memory is broken up into bytes with each byte having 8 bits. The bit is the smallest memory element, consisting of one 'switch' that can be 'on' of 'off''. More precisely, these on's and off's are represented by 'high' or 'low' voltages in the hardware for a bit. We tend to think of these on's and off's as 0's and 1's. In other words, each bit can hold a 0 or a 1, and, since a byte is a collection of eight bits, it holds some combination of eight 1's and 0's.

Usually, a character requires one byte of memory. The unique representation for 'a' we referred to above is thus one combination of eight bits while the representation for 'A' uses a different combination. Part IV, Section C of chapter 9 talks more about this and ASCII, the most commonly used code to represent characters in PC's.

An integer might require 2 bytes and a double might require 8 bytes. (The specifics depend on the hardware, the operating system and the software being used) Again, each value represented as an integer or double has its own unique representation - usually based on the binary number system.

Note that a variable of one type takes up more or less memory than a variable of another type. The amount of memory required of a type also indicates the range of values an instance of a type might have. For example, if an integer uses 2 bytes then there are 65,535 possible different values for an integer. (In the binary number system, 16 bits can represent 65,535 different values. ) Since the integer type represents both positive and negative values, the actual range is usually -32,768 to 32,767. In other words, a huge number such as 893, 245,564,789,100 could not be stored in the memory set aside for a 16-bit integer. For a full list of types and their ranges in the Borland C++ compiler, click here.

Note further that there is nothing in a memory location to indicate how one should interpret the contents of that memory. All you would see if you could look into a computer's memory is a series of 0's and 1's (really high and low voltages). It would take knowing that a particular memory location holds a character, an integer, a double (or even an instruction - which has a wholly different representation) before you could correctly interpret the contents of that memory location.

C. Variables in Programs
So, now we have three variable types. Let's see how this 'type' information is used in a program. Each variable that is needed in a program must be declared and defined before it can be used. First, the program must be told that the variable exists and what type it is - this is the declaration part. Then each variable must be defined, meaning that the computer sets aside the right amount of memory for the variable. The combination of declaring and defining a variable enables the computer to appropriately store information into and interpret the contents of each memory location. As we will use variables throughout most of this text, the declaration and definition processes occur at the same time so usually we will simply say declaration when we mean both declaration and definition. In chapter 4 the difference between the two concepts will be explored more, in the context of declaring and defining what are called functions.

Back to our simple summation program: We must declare (and define) our four variables - the three inputs and their sum. Suppose we decide that users will always enter integers. A brief analysis will tell us that the sum must also be an integer so we must declare four integers. The declarations would appear as follows - the semicolons at the end of each line are a part of C++:

int value1;
int value2;
int value3;
int sum;

Note that the four variable names we have chosen here (value1, value2, value3, and sum) could have been completely different. We could just have easily chosen to write:

 

int humpty;
int dumpty;
int horses;
int men;

However, meaningful names make a program more readable. There is nothing worse than trying to figure out what a piece of code is supposed to do - especially if it is your own code.

While the variables names can be anything we want, the word int must appear exactly as it is. This is the set of characters that the computer understands to mean that the following is a variable of type integer. Not only must we use the word int but it must be spelled in lower case. C++ is what is called case sensitive language, meaning that a word spelled with upper case letters is different than one spelled with some or all lower case letters. For that reason the following variables are all different:

value1, Value1, VALUE1, vAlue1, valuE1

Getting back to issues of type declarations. If we had decided that the variables should be doubles we would have written:

double value1;
double value2;
double value3;
double sum;

Although in this case, it might not have made any sense, we could have declared some of the variables to be integers and some to be doubles:

double value1;
int value2;
int value3;
double sum;

Since we are working with numbers it would be wrong to declare any of these variables to be of type 'char' or character. But, suppose the program required the user to enter their first and last initials. Then we would need two variables to hold characters and we could have declared them as:

char firstInitial;
char secondInitial;

By the way, a character variable can only hold 1 character. In other words, you have not yet seen how to declare enough memory to hold a full name or any other set of characters. Such collections of characters are called strings. After you have read and understood the rest of this chapter, you might want to learn about how you can use strings in your programs.

D. Semantics
Every instruction we will write has a semantics - the meaning of the instruction. In C++ programs this means the action or actions a computer will take when it executes the instruction in a program. Variable declarations then have a semantics summarized as:

Set aside enough memory for a variable of this type and call this memory
location ....

In other words, when a computer executes the line:

int value2;

it "sets asides enough memory for an integer and calls this memory location 'value2'."

Material in the "Essentials of C++"

Borland C++ types

Top of Section Main Menu Next Section