Title: Variables
Course: CPS 120: Introduction to Computer Science ~ Master (Aug/2015) OLD > Section: Learning Units

Variables:

- A valid identifier is a sequence of one or more letters, digits or underscore characters (_).
- In addition, variable identifiers always have to begin with a letter, or begin with an underlined character (_ ), but not with a digit.
- They cannot match any keyword of the C++ language nor your compiler's specific ones.

Visual C++ Variables

- Defines a place in memory to hold a value.
- Unlike Visual Logic, it must be declared which means you cannot use a variable that hasn’t been declared.

The 3 main characteristics of a variable are:

1. Name
2. Data type
3. Value

1. Name

- Identifies the place in memory where the variable is stored.
- Can have up to 255 characters.
- Must begin with a letter of the alphabet or an underlined character ( _ ).  After the first character, it can contain letters, digits, and underscores, but no spaces or other characters.
- Is case-sensitive so uppercase characters are distinct from lowercase characters.
- Cannot be a Visual C++ reserved word.

2. Data Type:

Defines the kind of data that may be stored in the variable.

- char – a single character in a single quote, such as 'H'
- int – integer, such as 12345
- bool – takes only true or false
- double – floating-point numbers, such as 34.78
- string – list of characters in double quotes, such as "Michigan"

Basic C++ Data Types Chart:

Data Type | Description | Examples
int
Integer in range:
–2,147,483,648  to   2,147,483,647
34
7800
-101
-25671
double
Real number in range:
–1.7E308 to –1.7E–308, or
0, or
1.7E–308 to 1.7E308
49.2
0.3458
777.
10233.809212
string | Zero or more characters | "WCC" "714 East           University"
char | Exactly one character | 'a' '%' '4'
bool | true or false | true (or 1) false (or 0)

3. Value:

- Is the value stored in the variable.
- Must match the data type of the variable.

Variable Declaration

- Usually appears near the top of a program.
- Defines the name and type of a variable.
- Has the form:
type identifier;
OR
type identifier = initial_value;

Variable Declaration Examples
int Index; | Declares a variable named Index to hold integer values.
double Salary; | Declares a variable named Salary to hold double values.
string LastName; | Declares a variable named LastName to hold string values.
char Letter = 'h'; | Declares a variable named Letter to hold a single character.
bool Flag; | Declares a variable named Flag to hold true/false values.