What is Variable in C Programming
Variable in C Programming is also called as container to store the data. Variable name may have different data types to identify the type of value stored. Suppose we declare variable of type integer then it can store only integer values.Variable is considered as one of the building block of C Programming which is also called as identifier.
[box]A Variable is a name given to the memory location where the actual data is stored.[/box]
Consider real time example , suppose we need to store water then we can store water in glass if quantity of water is small and Bucket is the quantity of water is large. And big can to store water having quantity larger than bucket similarly Variable (i.e Value container) may have different size for storing different verities of values.
Must Read : Fundamental Attributes of Variable
What is Variable in C Programming?
- 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
- Since the location ‘x’can hold Different values at different time so it is refered as ‘Variable‘
- In short Variable is name given to Specific memory location or Group.
Consider following Scenario –
Step 1 : Memory Before Variable Declaration
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 in C Programming
- Now we have declared a variable. (in this case we have declared character variable)
- 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 in C Programming
- We have Initialize character variable now.
- After initializing value inside that memory block gets overwritten.
- 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..