# Chapter 6 - Variables

## 6.1 Variables Defined

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 using namespace std; main() { int Age = 25; Age = Age + 5; cout << Age << endl; }``` 30

## 6.2 Variable Names

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.

## 6.3  Variable Types

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).

## 6.4  Using Integer Variables

The next program demonstrates using different int variable types.

 ```#include 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

Be careful when using integers that you do not assign a value above the largest legal value or below the smallest legal value.  If you do, the value will wrap around.  In the next program, we declare a short int and make it equal to its maximum legal value.  Incrementing (adding 1) the variable causes the value to wrap around and become its smallest legal value.

 ```#include 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++

## 6.5  Using Float Variables

For numbers that have a decimal point, the float variable type is used, as shown in the next program.

 ```#include 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 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

## 6.6 Using Char Variables

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 using namespace std; main() { char MyGrade = 'A'; cout << "My Grade is an " << MyGrade << endl; }``` My Grade is an A

## Exercises - Chapter 6

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