3. Variables

Sometimes a program needs to store a value as it executes. For example, it might need to store a number entered by the user or the result of a calculation. In order to store the value, the program needs a variable.

RAM and Program Execution

RAM, also called “memory”, stores a program’s instructions and data as the program executes. The organization of RAM can be thought of as a series of bytes with each byte identified by a sequential number called an address. For example, suppose a computer has 128 MB of RAM. Each of the 128 million bytes would be identified by the numbers, or addresses, 0 through 128 MB ‒ 1. Since the first address is 0, the highest address value will be one less than the total number of bytes available.

When we start a program, we are actually telling the operating system we want to execute the instructions in the program’s executable file. Immediately prior to the first instruction being executed, the operating system reserves (“allocates”) a block of memory for the program and loads a copy of the executable file into this memory. If the program stores any data, space within the block of allocated memory will also be reserved for the data.

What is a Variable? A Definition

“Variable” is the name for memory allocated to store a value as a program executes. For example, suppose a program asks a user to enter a number and 25 is entered. The program will need a place to store the 25 within the memory allocated for the program by the operating system. In order for memory to exist for this purpose, we, as programmers, need to ask for it when we write the program. We also need to say what type of data will be stored there. Continuing with our example, since the program is trying to store a 25, and 25 is an integer, we would ask for memory to be reserved to store an integer. Once we ask for memory to store a specific type of value, the memory cannot be used for another type of value. For example, we could not store 3.5 into the memory reserved for an integer.

Suppose, as a result of our request for memory, four consecutive bytes starting at an address of 100 are reserved to store the integer entered by the user. When 25 is read by the program, it will store the value in the four bytes because the program knows the address of the first byte. When we write the program, however, we will need to refer to the memory but we will not know the address. For example, after reading in the 25 we might want to display it on the screen in the next step. In order to do this, we would need to write the C++ code to have the program take the value at address 100 and display it. But we will not know the value of the address when we write the program. So how can we access the memory? We will assign a name to the memory allocated. Whenever we want to use the memory in the program we will use its name. Suppose we choose to name the memory allocated for the integer number because it stores a generic numeric value. Each time we want to access the memory in the program we will use the word number.

In summary, a variable is a named location in RAM reserved for a program to store a value of a specified data type. The term “variable” comes from the fact that the value stored in the memory can change, or vary, during the program’s execution. Note that RAM must be requested prior to the program attempting to store the value. Also, the amount of memory allocated for each data type varies (discussed further below).

How are Variables Created? Declarations

Now that we know what a variable is, how can we request the memory for a variable? We need to create a variable declaration. Here is the general form for declaring a variable:

data-type variable-name;

As noted previously, a variable is created to store a value of a specific data type. A variable declaration starts with the data type of the variable being requested. The C++ data types we will use are discussed in the next section.

The name of the variable follows the data type. Every variable must have a name which is used in a program to access the memory reserved for it. Some guidelines for selecting variable names are as follows:

GuidelineComments
Variable names should be meaningful.A variable’s name should describe its use or the meaning of its value. Avoid very short names and abbreviations as they are hard for humans to interpret.
Do not use a keyword for a name.The program will not compile.
Do not use a word from a library.The program may not compile.
Do not start the name with a number.The program will not compile.
Do not use a space within a name.The program will not compile.
Do not use special characters.If a character other than a letter, number, or underscore character is placed in a name, the program will not compile.
Do not use all uppercase letters.Names having all uppercase letters are reserved for a specific purpose (see the Constants section).
Follow the established standards.In a work environment, employers often set naming standards. This is also commonly done by instructors. See your instructor for your class’ standards.

Every variable in a program must be declared. If we try to use a variable without declaring it, the compiler will catch that the variable is not declared and the program will not compile.

If a program needs multiple variables of the same data type there are two options for their declaration. Suppose a program needs three variables to store integer values:

// Declare separately          // Declare in a single list
int first_number;              int first_number,
int second_number;                 second_number,
int third_number;                  third_number;

Note that each variable can be declared separately or the data type can be used once followed by a comma separated list of variables names terminated by a semicolon.

What Types of Values Can be Stored? Data Types

We have five data types to choose from:

Data TypeSample Values
char'A', 'a', '!', ' ', '5'
int4, -234, 0
float2.56, -.634, 9., 0.0
double2.56, -.634, 9., 0.0
booltrue, false

Character (char) variables store a single character and are capable of storing most of the characters which can be typed. As illustrated, this includes both uppercase and lowercase versions of letters, punctuation, the space character, and the digits 0 through 9. The single quotes are used to indicate a value is a character. Note that a digit stored in a character variable is treated as text, not a number to manipulate mathematically. One byte is allocated in RAM for character variables.

Integer (int) variables store numbers without decimal points and are allocated four bytes in RAM.

We have two data types for storing floating-point values: float and double. A floating-point value is a number with a decimal point. It is not necessary for the decimal point to have any digits following it. The difference between float and double is the amount of memory allocated in RAM for the data types. Four bytes are allocated for float and eight bytes for double. As a result, double variables can store much larger values than float variables.

Boolean (bool) variables have a value of true or false. The keywords true and false can be used to assign values to Boolean variables (e.g., result = true; where result is declared as a Boolean variable). One byte is allocated for Boolean variables.

How should the data type for a variable be chosen? A variable’s data type should match the values to be stored in the variable. For example, integer values could be stored in float and double variables because they could be treated as floating-point values with nothing after the decimal point. In reality, however, they aren’t floating-point values. So if integer values are going to be stored, the variables created to store the values should have the data type int as it is the best match for the data.

Can Variables be Given Values at the Time They are Created? Initialization

Variables are not given default values when declared. Whatever value the bytes have prior to being reserved for a variable remains unchanged and has no meaning to the current program. The value of a newly declared variable is, therefore, said to be “undefined” or “garbage”.

We might, however, want to give a variable a value when it is declared (called initialization). To initialize a variable, add an equal sign and the value to the declaration prior to the semicolon:

double price = 3.95;

In this example, the memory for the variable price will be assigned a value of 3.95 immediately after it is allocated.

Additional Points

Variables should be created for a single purpose. If a variable is used for multiple, different purposes, a program could become hard to understand. For example, suppose a variable named tax is used to store a sales tax rate of .075 but also used to store the taxes calculated for a purchase (e.g., 5.50). Going through the program, a reader would have to figure out what was stored in the tax variable each time it was used. This adds unnecessary complexity to the program. Two different variables should instead be declared with each having a single purpose.

If a variable is not needed, it should not be declared. Keep in mind that each variable declaration results in RAM being reserved for the variable. If the variable is not used, the reserved RAM is wasted. Also, declaring a variable and not using it could make the program harder to understand as it is expected that every variable declared has a purpose.