Variables & Constant In C With Real Life Example

Variable:

Variable is an entity whose value can vary (i.e. change) during execution of a program.

 int a = 20;

int a

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. printf("********* Example Variable *********\n");  
  5. int i, j;  
  6. i = 10; j = 20;  
  7. printf("\n value of i=%d & j=%d \n",i, j);  
  8. // change the value of variable  
  9. printf("\n change value of variable ");  
  10. i = 50;  
  11. printf("\n After changing value of variable i is : %d \t", i);  
  12. getch();  
  13. }  
main

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.

    bank

    atm

Constant:

Constant is an entity whose value cannot be changed throughout the execution of program.

const int j = 50;
constant

  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. printf("********* Example Constant *********\n");  
  5. const int i = 100;  
  6. printf("\n value of constant i= %d \n",i);  
  7. i = 50;  
  8. printf("\n%d \t", i);  
  9. getch();  
  10. }  
Output: Will give an error because we cannot change the value of constant variable.
  1. #include<stdio.h>  
  2. main()  
  3. {  
  4. printf("********* Example Constant *********\n");  
  5. const int i ;  
  6. printf("\n value of constant i= %d \n",i);  
  7. //i = 50;  
  8. printf("\n%d \t", i);  
  9. getch();  
  10. }  
output

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.

    fixed deposit

Summary:

This article will help fresher candidates to understand Variables & Constants  in C with Real Life Example.

Up Next
    Ebook Download
    View all
    Learn
    View all