Variables in C++






A variable is simply defined as named storage in the code. It means for storing our values in the memory, we have to first reserve some
particular space in the memory location, and for its identification, we have to provide a name to it for easy access. This is what we called
a variable. Let's check this with a simple statement from the code.

              
                int value = 100;
              
            
  • "int" is the data-type for our variable. This means we are going to use the variable of type integer.
  • "value" is the name of our variable.
  • 100 is the value which we have stored in the variable.

Here we are reserving the space in the memory location of size integer(4 bytes). Then we are giving the name of that memory location as
"value" and finally storing the value "100" inside that. So now onwards in the code during any calculation, we can use the variable name "value"
directly instead of the hardcode value "100". Also, the value stored in the variable can be changed during program execution depending on how
we are using that variable in the calculations.


Variables Declarations Syntax:

As a good coding practice, we should always define the variables with the name which will justify its purpose. This will also make our code
easy to understand. The name of the variable consist of lowercase or uppercase alphabets, numbers and underscore('_') character.
Ex. value_1, value_2 etc.
There are two style of declaring the variable.

  • Single variable declaration:
    Syntax is, < data-type>< variable_name>;
    Ex. int amount;
  • Multiple variables declaration: We can define the variables of similar type in a single line.
    Syntax is, < data-type>< first_variable_name>,< second_variable_name>,< third_variable_name>;
    Ex. int length, breadth, height;
         float value_1, value_2;

Varaible declaration vs Variable definition:

Variable declaration:   The main motive of a variable declaration is to tell the compiler that, this much amount of memory should be reserved
in the memory location before we are going to use the variable in our code.
Ex.

              
                int value;
              
            

Here we are directing the compiler that we are going to use the variable of integer type, so reserve 4 bytes of space in the memory, as
the size for integer is 4 bytes.

Variable definition:   When we are assigning the initial value to the variable then it is known as variable definition.
Ex.

                
                  int value;
                  value = 10;
                
              

Here the second line is the example of the varaible definition as we are assigning the value 10 to variable. Generally we do the
declaration and definition in a single line.
Ex.

                
                  int value = 10;
                
              

It is a good coding practice to always initialize the variable with some value during declaration because if we don't do so the uninitialized
variables will contains some garbage values.


Variable types in C++ :

As we have already covered earlier the C++ Data-types, so now depending on them C++ variables are also classified into different types.
Below are types of variables we generally use.

Data Type Keyword Details
Integer int For storage of the integer type of data. It provides 2-4 bytes of space in memory depending of CPU architecture.
Short short For storage of the short integer type of data. It provides 2 bytes of space in memory.
Long long For storage of the long integer type of data. It provides 4-8 bytes of space in memory depending of CPU architecture.
Long Long long long For storage of the extra long integer type of data. It provides 8 bytes of space in memory.
Floating Point float For storage of the single precision floating point data. It provides 4 bytes of memory space.
Double Floating Point double For storage of the double precision floating point data. It provides 8 bytes of memory space.
Long Double Floating Point double For storage of the long double precision floating point data. It provides 16 bytes of memory space.
Character char For storage of the charcter data. It provides 1 byte of memory space.
Wide Character wchar For storage of wide character data.
Boolean bool For storage of the boolean type data. It provides 1 byte of memory space.
Valueless or Void void For storage of data of no type.


Apart from these predefined datatypes, we are also allowed to define the variables of user-defined datatypes like enum, struct,
class, etc.


Const variable type in C++:

Sometime in our code we are in need that value of some variables should not get manipulated during any calculation and it should be
same throughout the code. It means the values of thoese variables should be constants. We can achieve this by using "const" keyword
during variable declaration. It's syntax is:
< const> < data-type> < variable-name> = value;

Note:- In case of const variables we should always assign the required initial value to the variable while variable declaration because we
are not allowed to change its value afterward. Let see the code example of the const variable

                
                  #include < iostream>
                  using namespace std;
                  int main() {
                    const int var = 10;
                    var = var + 5;
                    cout << "New value of var is:"<< var;
                    return 0;
                  }
                
              

The output will be:

              
                error: assignment of read-only variable ‘var’
              
            

Here in the line "var = var + 5;" we trying to modify and assign a new value to the constant variable "var" which is not allowed, so compiler
will throw an error at this line while compilation.


Integer type aliases:

C++ library also provides the supports of various integer type aliases. Details of them are presented as below.

Data Type Details
int8_t It is a 1 byte signed integer.
int16_t It is a 2 bytes signed integer.
int32_t It is a 4 byte signed integer.
int64_t It is a 8 byte signed integer.
uint8_t It is a 1 byte unsigned integer.
uint16_t It is a 2 bytes unsigned integer.
uint32_t It is a 4 byte unsigned integer.
uint64_t It is a 8 byte unsigned integer.
intptr_t It is signed integer of the size similar to pointer.
uintptr_t It is unsigned integer of the size similar to pointer.

Here we have also mentioned "signed" and "unsigned" qualifier for integer.


HandsOn Topics:

Try below programs for better understanding on variables topic:-

  • Write a program to declare the variables of different data-types and then check the size of each variables using "sizeof()" operator.
  • In a program declare varaibles of different data-types without initializing them. Then check the output of thoese uninitialized variables.