1.1 Variable
Suppose you have a glass of water in front of you. The function of this glass is to store water. So that we can use the water kept in the glass when we need it. This glass is actually a variable. Where water is the value of this variable called glass.

The function of variables in programming is to store values or data in computer memory. So that it can be used as needed. For example, we want to store a cricketer's two-day run in computer memory. In this case, we first need to store the value of the cricketer's 2-day run in the middle of the variable. It is done by giving the value with a = next to the variable. This looks like variable = value; But it has nothing to do with mathematical equations. Try to understand by looking at the example below.
There are 2 variables called dayOneScore
and dayTwoScore
. With int dayOneScore
we are telling the compiler that our variable will store an integer value. This is what we call declaring a variable. And int is the data type. Seeing that, the C compiler understands that there will be integer type data. If you notice, you have to use a semicolon after declaring a variable. Here we have assigned the value of dayOneScore
as 45 and the value of dayTwoScore
as 50.
We can change the value of this variable if we want. Such as:
Here, the value of variable a was initially 10, but later it was changed to 30 to assign its value to 30.
Variable naming
The name of the variable does not have to be arbitrary. It has some rules. The rules are:
Only English uppercase and lowercase letters (A ... Z, a ... z), digits (0,1,2 ... 9) and underscore _ can be used to write the name of a variable.
The first letter of the variable cannot start with a digit (0,1,2 ... 9).
Important Note: The name of the variable should always give something meaningful. It is easy to understand what is being said here. For example, writing firstName is much better than writing fn
Constants
If we want to create a variable where the value cannot be changed. Such as the value of pi (Pi = 3.1416). What can be done in that case? Very simple. We will just add const at the beginning of the variable. Here const means constant, the value of which will never change.
The double here indicates that this variable is a decimal number (real number). Here double is a data type.
What we have learned in this chapter
The function of a variable is to store value or data in computer memory.
The name of the variable must be preceded by its type.
There are some rules for naming variables.
If we do not want to change the value of the variable, we can use constants there.
Last updated
Was this helpful?