DEV Community

kiran v
kiran v

Posted on

What is variable in programming

Every single thing (file, data, etc.) we save on computer get stored in computer memory. But actual problem starts when we want that particular data or we want to access it, so as to identify that particular location in computer memory which holds the data we want, we need to give a distinct name to that location so that we can access that data any time by using the location name given.

For e.g. shopkeeper keeps food items in different containers in his shop and stick a paper, which has name of particular food item written on that paper, on that container. You might have seen this many times.

In this example container is a storage area and it can be of any size i.e. small, large. And food items like rice, wheat, etc. can be stored in that container (storage area).

Every container has a sticky paper on it which tells particular food item inside it. With respect to computer analogy, here you can consider the food items as data/value, container as a storage area/memory location.

Above scenario applies to the term β€œVariable”.

Definition:
Variable is referred as a location in memory in programming languages.

Illustration:
Variable is a location in memory

Image description

Value 10 is stored in the memory location and name b is given to that location.

We can store different values in a same variable.

Image description

E.g. if we have stored some value in b say 10, then it is possible that we can assign different value to variable b say value 20. So now previous value inside in variable b will be overwritten and it will hold a new value i.e. 20.

Variable can hold only one value at a time.

Top comments (0)