Variables are used by programs to store data. Their values can be set, and changed as often as needed. Before you can use a variable, it must be declared. When you declare a variable, you reserve a memory location on the computer and assign a name to it. This memory location holds the variable's value. The following line declares an integer variable named Age.
int Age;
Declaring an integer variable such as this reserves four bytes of memory for storing its value. An integer is a number without a decimal point - e.g. 3, 1492, -200, 0, or -1. Once the variable is declared, you may assign it a value. The following line is an example assignment statement that assigns the value of 25 to Age.
Age = 25;
You may declare a variable and assign it a value in one statement:int Age = 25;
You may declare multiple variables and assign them values in one statement (separated by commas):
int Age = 25, Weight = 150, IQ = 120;
The assignment statement below adds 5 to the existing value of Age, thus making it equal to 30.
Age = Age + 5;
Below is a completed program using the integer variable Age.
#include <iostream> using namespace std; main() { int Age = 25; Age = Age + 5; cout << Age << endl; } |
30 |
When declaring a variable, you assign it a name (e.g. Age). Legal variable names must begin with a letter or an underscore, and can contain any combination of letters, numbers, and underscores. Variable names cannot contain spaces or symbols (other than underscore). Variables cannot be named after C++ keywords such as for, while, int, float.
Legal Variable Name | Illegal Variable Name | Notes |
X |
int | cannot use reserved C++ keywords |
Exam2 |
2Exam | cannot begin with a number |
My_Age |
My Age | cannot contain a space |
HelloWorld |
HelloWorld! | cannot use symbols other than underscore |
When choosing a variable name, be descriptive. For example, if you are writing an accounting program, use variable names like Assets and Liabilities instead of A and L. Variable names are case sensitive - Apple is a different variable name than apple. There are a lot of naming conventions for variables - e.g. all uppercase, all lowercase, proper case (capitalize first letter of each word), and camel case (lowercase first word and capitalize additional words). Here are examples of camel case variable names: pyramidHeight, employeeStatus, checkingAccountBalance.
In the first program, we used the int variable type. An integer is positive or negative whole number (does not contain a decimal point). Whereas floating point variable types have a variable number of places past the decimal point such as 3.1, 3.14, 3.141, or 3.1415. The following table lists the different C++ variable types.
Variable Group | Variable Type Name | Memory Size | Legal Values / Precision | Comments |
integer |
int or long |
4 bytes | -2,147,483,648 to 2,147,483,647 | int and long are usually the same |
integer |
short |
2 bytes | -32,768 to 32,767 | use for small integers if you need to conserve memory |
integer |
long long |
8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | use to store numbers larger than 2 billion |
integer |
unsigned int or unsigned long |
4 bytes | 0 to 4,294,967,295 | use for numbers that cannot be negative |
integer | unsigned short | 2 bytes | 0 to 65,535 | use for small numbers that cannot be negative |
integer | unsigned long long | 8 bytes | 0 to 18,446,744,073,709,551,616 | use for non-negative numbers larger than 4 billion |
floating point | float | 4 bytes | The max value of a double depends on the number of significant digits being stored. | use for numbers with a decimal point such as 3.14 |
floating point | double | 8 bytes | The max value of a double depends on the number of significant digits being stored. | use for large numbers with a decimal point or for numbers with many places past the decimal point such as 3.14159265358979 |
boolean | bool | 1 byte | 0 or 1 (false or true) | use for things that are either true or false |
character |
char |
1 byte | one ASCII digit | use for storing one alphanumeric digit such as 'm' or '&' |
In choosing a variable type, you could use the type which requires the least amount of memory. As can be seen in the previous table, an int variable requires twice as much memory as a short int. Should you be concerned about saving two bytes of memory? For the programs in this book which only declare a few variables at most, saving memory is not a concern. More advanced programs may declare thousands or millions of variables and conserving memory could be a concer. If there is any chance that the value you assign to the variable will be larger than its type, use the larger variable type (e.g. use int instead of short int).
The next program demonstrates using different int variable types.
#include <iostream> using namespace std; int main() { short int Apples = 24, Oranges = 18; cout << "Total fruit = " << Apples + Oranges << endl; unsigned long long NetWorth = 1000000000000; cout << "I'm worth $" << NetWorth << endl; return 0; } |
Total fruit = 42 I'm worth $1000000000000 |
#include <iostream> using namespace std; main() { short int Test = 32767; cout << Test << endl; Test = Test + 1; // increment Test past max legal value cout << Test << endl; return 0; } |
32767 -32768 |
When you add 1 to the value of a variable, it is called incrementing the variable. When you subtract 1 from the value, it is called decrementing the variable. In the previous program, we increment the variable Test:
Test = Test + 1;
An easier way to increment Test is to do the following:
Test++;
You can also decrement (subtract 1) a variable by following its name with two minus sings:
Test--;
This is where the C++ programming language gets its name. The C programming language was the predecessor to C++. Therefore, the next version of C is C + 1, or C++
#include <iostream> using namespace std; main() { float A = 1.5, B = 2.44, C; C = A + B; cout << C << endl; } |
3.94 |
It is good practice to type a decimal point when dividing integers. The next program demonstrates how dividing an integer by an integer results in an integer - it will truncate (cut off) anything past the decimal point.
#include <iostream> using namespace std; int main() { float A = 1/2; cout << A << endl; float B = 1.0/2.0; cout << B << endl; return 0; } |
0 0.5 |
A char variable stores a single alphanumeric character. You use single quotes when assigning a char variable a value. The following program demonstrates this variable.
#include <iostream> using namespace std; main() { char MyGrade = 'A'; cout << "My Grade is an " << MyGrade << endl; } |
My Grade is an A |
What type of variable would be most appropriate for storing each of the following, keeping in mind to conserve memory whenever possible?
1. Someone's age
2. The balance of a bank checking account
3. The yearly budget for the U.S.
4. A student's letter grade in a class
5. A car's odometer reading
6. Whether or not someone has a driver's license
7. The distance in miles to other stars in the galaxy
8. The outdoor temperature