Variables & Constants

Let’s take a simple program to explain variables & constants


int main()
printf("The number is 5 \n");

return 0;

This program is not such a complex one, this is same as Hello World. But the difference is, it prints as ‘The number is 5’.

Now we need to print 10 instead of 5, not at compile time, it should be at run time. And we should not edit the code again and again to get the desired output.
Hope that now you understand the requirement, just think of it. Are you ready? can we achieve this? Yes sure. So, For that we are introducing an important concept in C Programming, Can you guess that? yep, that is Variable

Variable is a container which stores the data,Variable may have different DATA TYPES (int,char, float like that).


int numberToPrint;

numberToPrint is the name of a variable
int is DATA TYPE of a variable
This is an integer variable as its data type is int, And as it is int variable,  we can store integer values only.


Lets come back to our task, we need to change the value getting printed dynamically. We need to give input (a number that needs to be printed).
Here we add our variable into our program

int main()
int numberToPrint;              /// Variable Declaration
printf("enter number to print \n ");
scanf("%d",&numberToPrint);           /// getting input value at run time
printf("The number is %d \n,numberToPrint");      // prints output
return 0;

From the above explanation , you can understand the usage of variable easily. Then Lets see how Variable works.


Can you understand the meaning of that Line above?
Lets say,
int a;
Now, we have declared a variable.
Compiler checks the data type of that variable and allocates memory depending on that data type.
Initially no value is assigned to that variable , so the garbage will persist in that memory location.
Now, we are assigning a value to that variable like below

a = 10;

instead of garbage value, 10 is stored in that memory location.
Again, i am going to change the value of that variable like below,

a = 5;

Now, 10 is replaced by 5, but the memory address is same.Hope you understand the definition of variable:
To print the address of a variable , you can use like this ‘&var_name’.

Now lets back to our task.Now you can understand the program and you can enter your desired number at run time using scanf() function. It gets input and assign it in address of that variable.
printf() function prints the number what we have entered.

Memory Space will be created as datatype says,

int           4 bytes
short         2 bytes
char          1 byte
float         4 bytes
double        8 bytes
bool          1 byte

lets consider variable declaration as below

int numberToPrint  = 100;
Since its data type is int , it allocates 2 bytes in memory ,


  1. Variable name must begin with letter or underscore.
  2. Variables are case-sensitive
  3. They can be constructed with digits, letters.
  4. No special symbols are allowed other than underscore.

Lvalues and Rvalues in C:

There are two kinds of expressions in C:
lvalue : An expression that is a lvalue may appear as either the left-hand or right-hand side of an assignment.

rvalue : An expression that is a rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side.
int var = 100;


The constant refer to a fixed value and the program can not alter it during its execution. These are fixed values and called as literals.
The constants are treated just like regular variables except that their values cannot be modified after their declaration.

Types of constants:

  • integer constants.
  • float constants.
  • character constants.
  • string constants.


const int a = 10;

In the above line, value of ‘a’ can not be changed during run time, since it is readonly.


Leave a Reply

Your email address will not be published. Required fields are marked *