1.Variable and where can we find it!


      SOURCE CODE
  1. #include <stdio.h>
  2. int main()
  3. {
  4.                 int var;
  5.                 var=5;
  6. }



The code above is a simple program in which a variable “var” of integer type is declared in the first line of main function and then “var” is initialized with the value 5 with the help of assignment operator (=).



That’s all we can see on your  screen, now let’s dug a little deeper inside your machine and understand what is happening there in simple terms.
Now the program is compiling…….

 As soon as the first line gets executed (declaration of )your machine allocates or gives space to a memory portion in side your machine. I am drawing a box to represent the memory space allocated and the name of the box will be the name of the variable i.e ‘var’.


Now the time for the second line to be executed which will initialize ‘var’ with 5.This means, remember the box in the memory space allocated before will get filled with the value 5.

Now the thing is the ‘var’ is given by us to easily identify the memory space or you might say the location of the value 5 but inside your machine is not able to name it’s locations with alphabet instead all the memory locations are numbered and as soon as a variable is declared it get’s the memory space along with the the number.
To make our life easier take this instance,
Suppose you want to buy a house. When you buy the house you get to know the exact address of the house which will be most probably a combination of street number and area pin code which will be fixed by the government and you have no control over it but you can easily give a beautiful and unique name to your house. So here the the combination of pin code and street number is equivalent to the number given to a certain memory location inside your machine and name of your house is equivalent to the name of the variable.
Now suppose a world where every house is only allowed to have a unique name i.e, no two houses will have same name. And in this world your friends wants to come to your new house but they don’t know where to come. So you have two choices either you can tell them your street number as well as the pincode or you can also say the name of the house both will help them locate your house.


So here comes the main point of our story, lets assume our variable ‘var’ got allocated to a memory randomly  whose number is ‘1000’ this number is also known as the ADDRESS of the variable. So, now let’s modify our box diagram

 Now we are at a position from where we can find that 5 either by using the variable name ‘var’ or by the address of the variable ‘1000’.
With the variable name we can easily find out just by using the name itself but it’s not the case with the address we use a special device to locate the address which is known as POINTER in C.

Thank You.

No comments:

Post a Comment