Thursday, November 29, 2018

C++ Variables and Constants




Many times, we need to store some data in our program. How to accompany this? So, Hello guys, Welcome to this article.
In this article, we are going to learn about C++ Variables, Data-type of Variables, Declaring Variables and Declaring Constants. So let’s begin.

1. C++ Variables:

- A variable is a name given to a memory location.
Why do we need to give a name to a memory location?
- Every data we want to store will be stored in the memory. The memory is basically a group of cells and each of these cells have its own address associated with it. Whenever we request memory to store data, memory is allocated. So whenever we want to manipulate that data stored in that memory location, we will need the address of that memory location. There are certain problems associated with this.
i) We will need to always type the whole address whenever we want to access that particular data. Practically, the memory addresses are very long hexadecimal numbers, so it is tedious to remember the address.
ii) Generally, we manipulate a big amount of data in the programs, so it is practically impossible for anyone to remember the memory location address for each individual data and use them without getting confused.
iii) The memory is allocated from RAM whenever requested, and as the name suggests, it is Randomly Accessed Memory, and hence the memory address we will get for data is generated randomly, and it will never follow any pattern.

Here, a variable comes to rescue.
Whenever we want to store data, we declare a variable with a name. What happens here is that a memory block is allocated to store the data. The memory block allocated is given a name which is our variable name. So whenever we want to access that data, we just use the name of that variable. Isn’t it easy to remember a variable name rather than remembering that long hexadecimal address?


This is what basically a Variable is.

2. The Data Type of Variable

The Data type is a means to tell the compiler the type of data that a variable will store.
Why do we need to tell the compiler the type of data?
- Basically, different size of memory is allocated for the different type of data. So whenever we declare variable, we need to tell the compiler the type of data, so the proper amount of memory will be allocated to store that variable.

There are 5 data types supported in C++.
i) int
- It is used to indicate an integer type data. 2 bytes of memory is allocated for int type variable in a 32-Bit Operating System.
For example, the age of the user will be stored in an int type variable.
i) char
- It is used to indicate a single character type data. 1 byte of memory is allocated for char type variable in a 32-Bit Operating System.
For example, Gender of the user will be stored in a char type variable.

iii) float
- It is used to indicate floating point number type data. Floating point number means a number with decimal a point. 4 bytes of memory is allocated for float type variable in a 32-Bit Operating System.
For example, the area of the circle will be stored in a float type variable.

iv) double
- It is used to indicate floating point number type data with double precision. Double precision means it will contain more digits after decimal point. 8 bytes of memory is allocated for float type variable in a 32-Bit Operating System.
For example, the mass of electron will be stored in a double type variable.

v) void
- It indicates no data. It is not used as a data type for a variable. It is basically used as the return type for functions.



3. Declaring Variables

We shall follow the following syntax to declare a variable.

Syntax:
<data type>  <name of variable>   =   <initial value> ;

Syntax Explained:
-data type is the type of data which our variable will hold. E.g., int, char etc.
-name of variable is the name that we give to variable.
-initial value is an default value for variable, it is optional.

For example, if we want to declare a variable to store the age of the user, we will write it as
int age=18;
Here the initial value is optional and the variable can be also declared as:
int age;
And now we can access the Age of User by just using age variable.

But it is a good habit to always initialize variables while declaring.




4. C++ Constants and Declaring Constants

A constant is basically a data whose value cannot be altered later.
Syntax:
const   <data type>  <name of constant>  = <initial value>  ;
The keyword “const” keyword tells that the data is constant. An initial value is must while declaring a constant.
For example,
const float radius=3.2;
Now the value of radius cannot be altered anywhere in the program.



So this is a sample program to demonstrate Variables and Constants.


The Output of this program is,



So this was all about C++ variables and constants.


______________________________________________________

if any doubt or feedback please feel free to contact : 
    rahulrb15899@gmail.com

    +91 7775919753  (WhatsApp)

No comments:

Post a Comment

C++ Variables and Constants

Many times, we need to store some data in our program. How to accompany this? So, Hello guys, Welcome to this article. In this arti...