By it's definition, a variable is a place in memory that holds some information.
Some information may be numbers, characters, strings or booleans.
There are numerous ways to use and declare variables in C++. Here is one way to declare them:
type varName;
where, in the above, type is the appropriate data type of the variable
(i.e int, double, char etc...), varName is a useful name for your variable. This way will ONLY DECLARE IT and not give it any initial value. Be warned with this method as you may run into what is called garbage values (just random junk from memory. Don’t worry about that term for now).
This method below will declare the variable and initialize it to a given value:
type varName = initial_value;
where, in the above, type is the appropriate data type of the variable
(i.e int, double, char etc...), varName is a useful name for your variable, and the
initial value will vary when given the data type.
Here is a chart of the most used data types in C++:
| Type |
Description |
Size |
| char |
Character or small integer.
Signed range: -128 to 127
Unsigned range: 0 to 255
|
1 byte |
| short |
Short Integer.
Signed: -32768 to 32767
Unsigned: 0 to 65535
|
2 bytes |
| int |
Integer.
Signed: -2147483648 to 2147483647
Unsigned: 0 to 4294967295
|
4 bytes |
| long |
Long Integer.
Signed: -2147483648 to 2147483647
Unsigned: 0 to 4294967295
|
4 bytes |
| bool |
Boolean value. Either true or false.
Acceptable as 1 or 0 respectively.
|
1 byte |
| float |
Floating point number.
3.4e +/- 38 (7 digits)
|
4 bytes |
| double |
Double precision floating point number.
1.7e +/- 308 (15 digits)
|
8 bytes |
| string |
A special variable type holding successions
of characters between a pair of double quotes "".
(see tutorial 8 for more on strings).
|
---- |
Here is how we can declare and/or initialize some variables of different types in C++:
Let's see an example program to see how to use and work with variables.
Example 1:
Name and age of user
This program will ask for the name and age of a user and output the values they enter.
When the above program is executed, the name and age of a user will be stored in their appropriate variables and a message will be displayed at the end.
A sample run of the program may look like:
Enter your first name: Joan
Now enter your age (be honest): 42
Hello Joan. You are 42 years old.
There are different ways that variables can be declared:
Local variables
Local variables are quite the opposite.
They can be used in between two curly braces { } for a function.
Once the closing brace appears, the variables life span is over. Below is a sample of this:
The integer a and character v are local to the main function.
If you tried using them directly elsewhere, they would not exist.
Constant variables
Constant variables can be either local, static or global in the program.
The restriction is that they have the same value each time, hence being constant.
A common practice is to make the constant variable name all capital letters.
You cannot change the value of constant variable directly. So here is what something may look like:
The above snippet would produce an error upon compiling. It will tell you that you cannot change
the value of a constant variable. Take my advice: leave it alone.
Here is another snippet showing a constant: