Variables are named regions of storage within a C++ program. They are places where data can be stored and retrieved. A variable is introduced into a C++ program through a declaration that specifies at least 2 things: the type of the variable and the name of the variable. At all times, a variable will have a value, even if that value is indeterminate; thus the 3 things that a variable will always have is: a type, a name, and a value.
The type of a variable provides the way to interpret the data that is to be stored in that variable. The name of a variable provides a way for one to identify that variable in later parts of the C++ program.
Here are the types that one should know for now:
Type | Description |
---|---|
bool | Boolean, for storing the value true or false |
char | Character, for storing a value such as: 'A', '1', '?', or ' ' |
int | Integer, for storing a value such as: 42, -7, or 0 |
double | "Double precision floating point", for storing a value such as: 3.4, -9.81, or 0.0 |
string | For storing a sequence of characters such as the value "Hello world" |
A value expressed directly (such as: true, 'A', or 42) is known as a literal. There are boolean literals, character literals, integer literals, floating-point literals, and string literals.
Boolean literals are of type bool. The other 4 kinds of literals can have their type modified through prefixes or suffixes, but their default types are: char for character literals, int for integer literals, double for floating-point literals, and a type that is compatible with string for string literals.
A variable stores a value and a value can be expressed as a literal. Whenever one attempts to store a value into a variable, one must make sure that the type of the value and the type of the variable are compatible.
The following is a simple declaration for a variable:
int x;
This declaration allocates a region of storage that is large enough to store any value of type int, the identifier x is then introduced as the name of this storage. The value of this variable depends on the context of where it was declared; for now, one should assume that a variable has an indeterminate value unless one explicitly assigns a value to it first.
A variable can be assigned a value using an assignment operator. This can be performed within a simple declaration:
int x = 0;
Now x is guaranteed to have the value 0. Simple assignment can also be performed after the variable has been declared and the operand that can be used for assigning can include: a literal, a variable, or an expression.
int a = 0;
int b = 4;
int c = a;
a = b;
b = 7;
When the source code above executes, the values of the variables will be: 4 for a, 7 for b, and 0 for c.
All variables and other named entities have a scope, which determines the portion of source code in which that name is valid.
Study the following source code and the comments included with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include "std.h" int a = 0; /* Scope of a begins */ int main() { int b = 2; /* Scope of first b begins */ { int b = 3; /* Scope of second b begins, interrupting the scope of the first b */ int c = b; /* Scope of first c begins */ int d = 5; /* Scope of d begins */ cout << c << endl; /* Prints 3 */ } /* Scope of second b, first c, and d end. Scope of first b resumes. */ int c = b; /* Scope of second c begins */ cout << c << endl; /* Prints 2 */ } /* Scope of first b and second c end */ /* Scope of a ends at the end of this file */ |
The insertion of endl into cout has the same effect as inserting a new line character into cout.
Variable a is said to be in global scope, which means it's potential scope begins at the point of declaration and ends at the end of the source file.
The other variables all have block scope because they are all declared inside some block, which is a brace-enclosed sequence of statements.
Variable b was declared in the block that is also the function body of main; b's potential scope ends at the end of the main function. However, there is a nested block inside main's function body; the declaration of another variable named b interrupts the scope of the previously declared b. Within this nested block scope, any appearance of b refers to the second b variable. After the nested scope ends, the scope of the first b resumes.
The second c variable is an entirely different variable from the first c because the first c already expired at the end of the nested block. Since the second b is also out of scope, the second int c = b; declaration assigns the value of the first b.
The actual scope of a variable is the same as it's potential scope unless that variable's scope becomes interrupted by another entity with the same name within a nested scope.