What is Variable in C Programming
- A variable is named location of data.
- In other word we can variable is container of data.
- In real life we have different container for different purposes , like Water Can for storing drinking water , Bucket for storing water. similarly in C we have different containers for storing different data types.
- Size of container may varies for different data types.
- Integer can be stored in integer bucket [Integer bucket is nothing but "Integer Variable"]
- Initially 5 is Stored in memory location and name x is given to it
- After We are assigning the new value (3) to the same memory location
- This would Overwrite the earlier value 5 since memory location can hold only one value at a time
Variable :
- Since the location ‘x’can hold Different values at different time so it is refered as ‘Variable‘
- In short is name given to Specific memory location or Group.
Consider following Scenario -
Step 1 :
- Initially before declaring variable ,We have Only memory.
- Memory is block of bytes.
- Each byte is filled with random or garbage data.
Step 2 : Declaring Variable
- Now we have declared a variable. (in this case we have declared character variable)
- After declaring variable Compiler will allocate space for variable inside memory.
- Compiler Checks data type . Depending on data type it will allocate appropriate bytes of memory. (in this case Compile will allocate 1 byte because we have declared Character data type)
- It’s only declaration so , garbage value inside that address remains the same.
Step 3 : Initialize Variable
- We have Initialize character variable now.
- After initializing value inside that memory block gets overwritten.
Conclusion :
- Variable Name always holds a single Value.
- Variable Name is user defined name given to Memory Address.
- During Second Run , Address of Variable may change.
- During second Run , Value set inside variable during current will be considered as garbage..