Variable:
Variable is an entity whose value can vary (i.e. change) during execution of a program.
int a = 20;
- #include<stdio.h>
- main()
- {
- printf("********* Example Variable *********\n");
- int i, j;
- i = 10; j = 20;
- printf("\n value of i=%d & j=%d \n",i, j);
-
- printf("\n change value of variable ");
- i = 50;
- printf("\n After changing value of variable i is : %d \t", i);
- getch();
- }
Real life example of variable
- Everyone has a saving / current bank account. From this account you can withdraw money from ATM or deposit money by visiting the bank.
Constant:
Constant is an entity whose value cannot be changed throughout the execution of program.
const int j = 50;
- #include<stdio.h>
- main()
- {
- printf("********* Example Constant *********\n");
- const int i = 100;
- printf("\n value of constant i= %d \n",i);
- i = 50;
- printf("\n%d \t", i);
- getch();
- }
Output: Will give an error because we cannot change the value of constant variable.
- #include<stdio.h>
- main()
- {
- printf("********* Example Constant *********\n");
- const int i ;
- printf("\n value of constant i= %d \n",i);
-
- printf("\n%d \t", i);
- getch();
- }
Real life example of constant - Now suppose we want to create a fixed deposit in bank. This is done to keep a particular amount locked for some specific period.
Summary:
This article will help fresher candidates to understand Variables & Constants in C with Real Life Example.