Variable in C++ Programming with Example
C++ Basic Concepts
In this lesson, we will learn about the variable in C++ programming and its application, along with an example.
Definition of Variable in C++
A variable in C++ is just a human-readable name used to store data in computer memory. A variable is also used in a computer program to store, read and change the data in computer memory.
A variable is also known as Identifier because it is used for identifying values in a computer program.
When you declare a variable in C++ programming language, it means that you are asking the operating system to reserve a piece of memory with that variable name for storing data.
What is data?
Data is a collection of facts or figure or combination of both (facts and figure).
- Facts - Any word that is consist of only alphabets is known as Facts. Example: john, mango, book, etc these words are consist of only alphabets. So each of them is a data but consist of Facts.
- Figure - Any number whether it is whole number or real number is a Figure. Example: 15, 84.13, 5486, etc. So each of them is a data but consist of Figure.
- Combination of Both (Facts and Figure) - Anything which is consist of Facts and Figure is known as Alphanumeric Text. Example: 13street, alpha56, dx14c5, etc. So each of them is a data but consist of Alphanumeric Text.
Let's see an example of variables and how it is used to store data in the computer's memory.
a = 5 b = "Apple" a = 25
In the above example, a is used as a variable name to store the value (data) 5 in it. So we can say that the value of the variable a is 5. On line number 2 we have used another variable b to store the value (data) Apple (a value can be a number or text) so the value of the variable b is Apple.
On line number 3 we have changed the value of the variable a by storing a new value 25 in it. Now the new value of variable a is 25. So always remember that a variable can store only one value at a time if you store any other value in it then the previous value will be erased and the new value will be stored in it.