Previous Table of Contents Next


Chapter 3
Variables and Constants

Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values.

In this hour you learn:

  How to declare and define variables and constants
  How to assign values to variables and manipulate those values
  How to write the value of a variable to the screen

What Is a Variable?

You might remember variables from high school (let InterestRate = .75).

From a programmer’s point of view, a variable is a location in your computer’s memory in which you can store a value and from which you can later retrieve that value.

To understand this, you must first understand a bit about how computer memory works. Your computer’s memory can be thought of as a series of cubby holes, all lined up in a long row. Each cubby hole—or memory location—is numbered sequentially. These numbers are known as memory addresses.

Variables not only have addresses, they have names. For example, you might create a variable named myAge. Your variable is a label on one of these cubby holes so that you can find it easily, without knowing its actual memory address.

Figure 3.1 is a schematic representation of this idea. As you can see from the figure, we’ve declared a variable named myVariable. myVariable starts at memory address 103.


Figure 3.1  A schematic representation of memory.

RAM is Random Access Memory. When you run your program, it is loaded into RAM from the disk file. All variables are created in RAM as well. When programmers talk of memory, it is usually RAM to which they are referring.

Setting Aside Memory

When you define a variable in C++, you must tell the compiler not only what its name is, but also what kind of information it will hold: integer, character, and so forth. This is the variable’s type. The type of the variable tells the compiler how much room to set aside in memory to hold the variable’s value.

Each cubby is one byte large. If the type of variable you create is two bytes in size, it needs two bytes of memory, or two cubbies. The type of the variable (for example, int) tells the compiler how much memory (how many cubby holes) to set aside for the variable.

Because computers use bits and bytes to represent values, and because memory is measured in bytes, it is important that you understand and are comfortable with these concepts.

Size of Integers

A char variable (used to hold characters) is most often one byte long. A short int is two bytes on most computers; a long int is usually four bytes, and an int (without the keyword short or long) can be two or four bytes. If you are running Windows 95, Windows 98, or Windows NT, you can count on your int being four bytes as long as you use a modern compiler.

Listing 3.1 should help you determine the exact size of these types on your computer using your particular compiler.

Listing 3.1 Determines the Size of Variable Types on Your Computer.


1:  #include <iostream.h>
2:
3:  int main()
4:  {
5:    cout << “The size of an int is:\t\t”    << sizeof(int)
      ⇒<< “bytes.\n”;
6:    cout << “The size of a short int is:\t” << sizeof(short)
      ⇒<< “ bytes.\n”;
7:    cout << “The size of a long int is:\t”  << sizeof(long)
      ⇒<< “ bytes.\n”;
8:    cout << “The size of a char is:\t\t”    << sizeof(char)
      ⇒<< “ bytes.\n”;
9:    cout << “The size of a bool is:\t\t”    << sizeof(bool)
      ⇒<< “ bytes.\n”;
10:   cout << “The size of a float is:\t\t”   << sizeof(float)
      ⇒<< “ bytes.\n”;
11:   cout << “The size of a double is:\t”    << sizeof(double)
      ⇒<< “ bytes.\n”;
12:
13:     return 0;
14: }

The size of an int is 2 bytes.
The size of a short int is 2 bytes.
The size of a long int is 4 bytes.
The size of a char is 1 bytes.
The size of a bool is 1 bytes.

The size of a float is 4 bytes.
The size of a double is 8 bytes.
On your computer, the number of bytes presented might be different!

Most of Listing 3.1 should be familiar. The one new feature is the use of the sizeof() function in lines 5 through 10. sizeof() is provided by your compiler, and it tells you the size of the object you pass in as a parameter. For example, on line 5 the keyword int is passed into sizeof(). Using sizeof(), I was able to determine that on my computer, an int is equal to a short int, which is two bytes.


Previous Table of Contents Next