scope,visibility and life time of a variable in C language


Life time: Life time of a variable in c is the alive state or life of a variable in the memory.
It is the time for which a variable can hold its memory. It is the time or life of a variable how long does the variable holds in the memory. The lifetime of a variable is automatic and static. The life time of a Local variable remains active till the end of program is called static variable.

The following example given is a simple implementation of local variable and global variable in C programming.
The variable that is declared in a function or code is called a local variable.A local variables cannot be used outside the function in which They are declared. The scope of Local variables remains within the defined function body or block of code only.

Program of local variables in C.

#include <stdio.h>
void employee()
 {
   // Local Variables of the function
   int salary= 20000;
   float bonus = 0.45;
   printf("\nEmployee Salary is = %d \n", salary);
   printf("\n Employee Bonus is= %f", bonus);
 }
int main()
 {
   employee();
   return 0;
}

Output:
Employee Salary is =20000
Employee Bonus is=0.450000

Explanation:In the program given above, inside the the definition of employee() function the variables are declared and initialized. The local variable salary has declared as int data type and assigned the value 20000 similarly the variable bonus has declared as float and assigned value 0.45. The scope and life of these variable remains local to the employee() function and cannot be used outside the function.
Inside the main function the employee() function is called and gives the following output.
Employee Salary is =20000
Employee Bonus is=0.450000


Global Variable:The variable whose scope remains through the entire program is called global variable in C. Global variables are not defined inside any function and can be accessed and used in any function.

Program of Global variables in C.

#include <stdio.h>
//Declaring Global Variable
   int salary= 20000;
   float bonus = 0.45;
void employee()
 {
   printf("\n Details inside employee() function");
   printf("\nEmployee Salary is = %d \n", salary);
   printf("\n Employee Bonus is= %f", bonus);
 }
int main()
 {
   employee();
   printf("\n Details inside main() function");
   printf("\nEmployee Salary is = %d \n", salary);
   printf("\n Employee Bonus is= %f", bonus);
   return 0;
}

Output:
Details inside employee() function
Employee Salary is =20000
Employee Bonus is=0.450000

Details inside main() function
Employee Salary is =20000
Employee Bonus is=0.450000
Explanation:The variables salary and bonus has declared and initialized outside the function's definition. These variables are global whose values can be used and accessed within or outside the function and any where in main() function. .


Variables Types on the Basis of Lifetime or Storage Class in C.

There are 4 types of keyword variables in C programming. These Variables use the special keywords before their definition. These keywords are reserved word and have specific meaning and purpose. The keyword defines the use and the purpose of the variable. The scope of these variable will be global, local or both depending on type of variables.
a.Static Variable:
The Static is the keyword in C language. Static keyword is used to define static variable. The scope of static depends on the area of its declaration. The scope of variable is local If a static variable is defined within a function. If the a variable declared outside the function, its scope is global. The lifetime of the static variable remains throughout the program and its lifetime. Static variable holds its value whenever a program is called. The default value of the static variable is 0.
Syntax:
static data_type variable_name = initial_value;

C program demonstrate static keyword.

/*
The program given below increase the value of static variable 10
every time when the function is called.
*/
#include <stdio.h>
void inc_val()
{
  int p = 20; // Local variable declaration
  static int b = 30; // Static variable
  p = p + 10;
  q = q + 10;
  printf("\nThe value of local variable p=: %d ", a);
  printf("\nThe value of Static variable b=: %d ", b);
}
int main()
{
  inc_val();
  printf("\nCalling inc_val function 2nd time ");
 inc_val();
 printf("\nCalling inc_val function 3rd time ");
  inc_val();
 return 0;
}

OUTPUT :
The value of local variable: 30
The value of Static variable: 40
Calling function 2nd time
The value of local variable: 30
The value of Static variable: 50
Calling function 3rd time
The value of local variable: 30
The value of Static variable: 60

Previous Topic:-->> Recursion Functions in C || Next topic:-->>Scope,visibility and Life time of variable.