We already know that data is stored in memory. So variables allow you to name the storage for your data. Therefore, you as a programmer can create variables.
Each variable must have a specific data type. (you can review data types). The data type determines the size of the variable. That is the size of memory it occupies.
To name a variable, there are some rule to follow:
- variable name must begin with a letter or an underscore
- cannot begin with a number or special character
- names are case-sensitive
Examples of variables are: score, student, num1, _var
Variable Types
This is about the same as Data Types. The basic types of variables in C++ are listed below:
S.No | Type and brief Description |
---|---|
1 | bool Stores either value true or false. |
2 | char Typically a single octet (one byte). This is an integer type. |
3 | int The most natural size of integer for the machine. |
4 | float A single-precision floating point value. |
5 | double A double-precision floating point value. |
6 | void Represents the absence of type. |
7 | wchar_t A wide character type. |
Now, the types in the list above is also known as primitive types. However, C++ also provides other types of variables. These include:
- enum
- Pointer
- Array
- Reference
- Collections
- Classes
We would examine these in subsequent lessons
Defining and Initializing a Variable
Before you can use a variable, you must define it. This simply means you provide a name of the variable, then you provide the data type as well. In this way, the compiler reserves the right amount of space for the variable.
Examples of how to define a variable is given below:
int a, b c; //declares the variables a, b, c as int float score; //declares the variables score as float char grade; //declares the variables grade as char double amount, salary; //declares the variables amount and salary as double
Initializing a Variable
This means assigning an initial value to the variable. This can be done along with the declaration. It could also be doneĀ in another line. The code below declares and initialized the same variables.
int a = 10, b = 20, c = 30; float score = 99.3; char grade 'A'; double amount 8000, salary = 25000;
If however, you declare a variable without initializing it, then the value is set to NULL for static storage. For others, the value is undefined. So it’s always good to declare your variables.
Why Variables are Declared
Declared variables reduces the compilation time. This is because, the compiler knows that there is a variable of the specified type, then linking can proceed.
Also, when multiple files are used, then variable define in one file would be available in another file. This happens during the linking phase.
However for this to work, you need to declare the variable using the extern keyword. This you do in just one place.
Declaration vs Definition Example
The example below illustrates the difference between variable declaration and definition.
//Variable declaration vs definition //By Kindson The Genius #include <iostream> using namespace std; // Variable declarations: extern int x, y; extern int z; extern float w; int main () { // Variable definitions: int x, y; int z; float w; // initialization x = 10; y = 20; z = x + y; cout << z << endl ; w = 70.0/3.0; cout << w << endl ; return 0; }
If you run the code above, then you will have the output:
30 23.333
Lvalues and Rvalues
You probably will hear of these only in C++!
Lvalues and Rvalues are the two types of expressions in C++. So what are they?
- lvalue – these is an expression that refers to a memory location. It can appear in an assignment statement. Either in the left or right hand side.
- rvalue – these is a value that is stored in some memory address. It only can appear on the right hand side of an assignment. Therefore, no value can be assigne to it.
For example:
int age; age = 37; // age is and lvalue, 37 is an rvalue
Variables are rvalues and therefore can appear on any side of an assignment statement. However, literals like numbers are lvalues.