“Control all the variables you can… Don’t worry about the rest” Forrest Griffin
Programs work with data. A calculator takes data from a user and does the mathematical operation on them. A video player takes video-data and displays them to the user. A browser takes the address of a website from a user and then will load the content of that website.
But before a program starts to work on a data, the target data needs to be stored in the memory temporarily.
In C++ language, we store these data by creating variables.
This is the structure of how we create a variable:
Data-type variable-name;
-
Data-type: you see, in programs, data has
different type. For example if the target value is an integer number
like 1, 2, 4, 222, 4323 etc. we consider the type of these numbers as
`int`. Or if the target value is a number with fraction part like 33.233
or 322.23423, we call these types of values as `double`. But why we need
to declare a type for a variable? This is because data-types take
different memory-spaces. For example a variable of type `int` is big
enough to hold the range of values from -2,147,483,648 to 2,147,483,647.
So if we want the target variable to hold a large value, we might need
to choose a data-type that will cause a larger amount of space in the
memory to be set aside. Or if we know that the target variable won’t
store a large value, we can choose a data-type that only takes a small
portion of the memory.
For example:
int iVar;
The iVar here is the name of the variable and its type is int which means the memory space of this variable is big enough that allow us to store integer values range from -2,147,483,648 to 2,147,483,647 in this variable.
Also the data-type of a variable helps the compiler to figure out what type of values is allowed to be stored in the memory location pointed by the variable. For example if we attempt to store values of different data-type in a variable, the compiler will try to cast the value to the right data-type or it will return error if it couldn’t do the casting.
-
Variable-name: the variable-name is used to
access the memory location assigned to that variable. It’s like a door
that opens to the memory-space. With the help of this name, we can store
values in the memory and get values out of that memory-space.
Example:
#include <iostream>
using namespace std;
int main()
{
int iVar;
iVar = 100;
cout<<iVar;
return 0;
}
Output:
100
In this example, we’ve created a variable of type int with the name of iVar. So now the compiler set aside a portion of the memory space for this iVar variable and only the iVar variable can access this memory location.
In the next statement of the example above, we stored the value 100 to the memory-space of the iVar variable with the help of assignment = operator.
Basically when we want to store a value in a variable, we set the name of that variable on the left side of the assignment = operator and the target value on the right side of this operator.
This way, the value will be stored in the memory location of the target variable.
If we wanted to get the value stored in the memory-space of a variable, we can simply use the name of the variable just like the way we did in the ` cout<<iVar;` statement.
We know that << operator of the cout object, takes a value and send it to the output. Now on the right side of this operator we put the iVar variable and the value of this variable will be sent to the output.
There are some rules when we want to set a name for a variable:
-
The name that we use for a variable can contain letters, numbers
and underscore (_). For example: jack, ellen2020, _black, sunnySeason
etc. -
A name for a variable can begin with either underscore or letter.
For example: _sum, check_point etc. -
We can’t use white space and special characters like !@#
%^&*()+ etc. in variable’s name. For example these names are
illegal: ell en, %%jack, Tony&&, pre-process. -
The C++ language is case sensitive which means a variable named
`ellen` is different than a variable named `Ellen` or `ELLEN`. -
We can’t create two variables with the same name in the same
function. But we can have two variables with the same name each
live in different function or one live in one function and the other
live in global (outside all other functions). -
We can’t use C++ language reserved keywords as the name of a
variable.
Here’s the list of reserved keywords:
| auto | extern | short | while |
|---|---|---|---|
| break | float | signed | _Alignas |
| case | for | sizeof | _Alignof |
| char | goto | static | _Bool |
| const | if | struct | _Complex |
| continue | inline | switch | _Generic |
| default | int | typedef | _Imaginary |
| do | long | union | _Noreturn |
| double | register | unsignd | _Static_assert |
| else | restrict | void | #_Thread_local |
| enum | return | volatile | string |
-
Semicolon `;`: the process of creating a new variable is called
`declaration statement` and any declaration statement ends with
semicolon `;`.
Note: We only use data types once when we’re declaring a variable. From that moment afterward, assigning values to a variable does not need mentioning the type of the variable anymore.
For example:
int firstNumber;
firstNumber = 100;
As you can see the second line did not include the type of the variable.
We can also initialize a variable right where we declare it.
Example:
int firstNumber = 100;
Variables can be declared inside a function which in that case they are considered local variables. Or they can be declared globally (outside of all functions).
If a variable is declared locally, the variable is only accessible from the point of declaration to the end of that function only. Basically no-other functions can access the variable.
On the other hand if a variable is declared globally, within the body of any function in the source code we can access the variable and get its value or assign a new value to that variable.
Example:
#include <iostream>
using namespace std;
int age;
int height;
void printValues(){
cout<<age<<", "<<height;
}
int main()
{
age = 100;
height = 6;
printValues();
return 0;
}
100, 6
In this example, we’ve created two global variables named age and height. And these two variables are accessible within the body of both main and printValues functions.
But take a look at the example below where we tried to access two variables that are declared within the body of main function, in the secondFunction():
#include <iostream>
using namespace std;
void secondFunction(){
cout<<age<<", "<<height;
}
int main()
{
int age = 100;
int height = 6;
printValues();
return 0;
}
Output:
error: ‘age’ was not declared in this scope
cout<<age<<" "<<height;
Within the body of the main function we declared two local variables and then called the secondFucntion(). But because within the body of the secondFucntion we tried to access two variables that wasn’t declared neither globally nor inside the body of secondFunction itself, we got compile time error.
Notes:
-
A function can only see and work with variables that exist
globally or within the body of that function. Check the function
section for more information. -
Also the name of a variable should be unique in the scope that is
declared. For example if a variable is declared globally with the name
`gVar`, no other `gVar` is allowed to be declared globally. Similarly,
if within the body of a function like `main()` we declared a variable
with the name say `age`, no other variable with this name is allowed to
be declared inside this function.
Note: But we can have two variables with the same name one declared globally and the other declared locally within the body of a function. Also there can be two or more local variables with the same name if each one of them is declared inside a separate function.
Multiple variable declarations:
We can declare multiple variables of the same type in a single statement as well:
Data-type variable_one, variable_two, variable_n;
In this structure, each variable is separated via colon ,.
For example:
double v_one, v_two, v_n;
In the example above, we’ve created 3 variables of type double.
Note: we could also assign values directly to each variable if we wanted.
For example:
int variable_one = 10, variable_two = 30, variable_n = 50;