what are variables?

Hello coders! Welcome back.

So what are variables? For the answer imagine a glass of water.














The glass is equivalent to the variable. And the water inside is equivalent to the value, the variable is holding.

Variables are nothing but names.

These variables directly point to the memory location.

Before using the variable inside the program we need to declare it.

what is declaration? a declaration is nothing but announcing the properties of the variable to the compiler, properties like how much space a variable is going to use, etc

This is how we declare a variable-












A variable with name "varr" is declared of data type int, which means integer.

Data type means, how much space a variable is going to occupy in the memory.

In the above example -
The variable "varr" is going to occupying 2 bytes of memory because 'int' holds 2 bytes of memory.
Similarly, 'float' holds 4 bytes of memory. There are many types of data types.

In the end, we need to put a semi-colon by this the compiler differentiates between the current line and next line to execute. And it's mandatory to put at the end of each instruction.   

We can also initialize the variable as -

int varr=4; 
we can change the value of the variable in our program when required.

As the name itself suggest vary-able which means something changes over time.

How we can change the value?











Here we need not mention int once again. It indicates that we are allocating memory once again.

I hope so you got an idea about variables used in C.

happy learning!


Comments

Popular post