A variable is a place holder in computer memory which has a
name and a datatype variable can have a value from the beginning but its value
is changeable later. We use variables to gain access to specific computer
memory addresses by a name.
Structure of variables in C++:
DataType VarName = Value; // variable
Data Type:
Data type represents the type of the data that we are going
to store in a memory address, and in C++ we have to write the data type for
every single variable that we make, we cannot change variable data types in
runtime, and we cannot store a data which its type dose not match with variable
data type.
Here we will see some C++ data types
Numeric
· Int :
int(integer) holds number without decimal
points
· Float :
float(floating point) holds numbers with
decimal points
· Log :
long holds big numbers without decimal
points
· long long :
long long holds very big numbers without
decimal points
Note: we can use signed
and unsigned keywords before each of the above data types to make them
accept signed and unsigned number (by default it is signed).
Alphabetic
· Char :
char(character) holds a single symbol it
can be anything like alphabet, number and another symbol but it the value for
char data type must be inside single citations (‘ ’). Any number inside single
citations will be treated as a symbol and we can’t perform mathematic operation on it.
· String :
string holds collection of characters(Symbols) same like characters it can be anything
(numbers, alphabets and other symbols) but values of the stings in C++ must be written inside double citations (“ ”), again number inside double citations will be treated as a symbol and we can’t perform mathematic operations on it.
· Bool :
bool ( Boolean ) holds true( 1 ) or false( 0
) values in it.
Variable name:
Variable names are the way for us to reach into a specific
memory address any time and as many times that we want without memorizing the
actual address of the memory. variable names have some rules for them :
· Variable name can’t start
with a number.
· Variable name can’t contain
spaces instead we can use underscore ( _ ) symbol or use camel case ( VarName
).
· Variable name can’t be a
reserved keyword ( int , if , for … ) of C++ .
· Variable name can only use
one symbol and that is underscore ( _ )
Assignment operator:
Assignment operator moves the right-side value into left
side variable. We will cover operators later with more details.
Value:
Value is what a variable is holding inside it. The value
data type must be the same data type which we wrote for our variable otherwise
we have an error. A value for a variable can be a name of other variable or
result of a mathematic operation or anything that can return same datatype.
We can simply write an ( { } ) square brackets to initialize
a variable to null. If we don’t initialize our variable:
DataType VarName; // uninitialized variable
It will return garbage( anything which is in that address
before ) for us and it can be anything.
Constant variables:
We can make variables constant just by adding const
key word before its data type so it will become read only on runtime:
const
DataType VarName = Value; //
constant variable
R-Value:
The right side of an assignment is called R-value. R-values
are not addressable they are stored in memory temporary without any name.
int num
= 7; // here 7 is an R-value
L-Value:
the left side of an assignment is called L-value. L-values
are addressable and they are stored in memory as much as we need them and they
have name so we can access them any time;
int
num = 7; // here num is a left
value
referencing:
we can make two types of references:
L-value reference
int &num =
VarName;
L-value references doesn’t store
values they store addresses like address of a variable, we ca access that
variable later by both its name and its reference name like if we assign a
value to reference the actual variable value will change too because they both
are pointing to same memory address.
·
R-value reference
int
&&num = 7;
R-values stores temporary values
which don’t have any name to access them later like 7 in here, when we change
an R-value reference in fact we changed that temporary value in memory.
No comments