What is the meaning of Static variable in C Programming Language?


What is the meaning of Static variable in C Programming Language?

A static variable is a variable that has been allocated statically. A local static variable exists for the period of program but it is only visible in the function body. If the function is declared as static the it is invisible to outer files. If we use static global variable in the file then it is limited to with in the file. 

A program in C:-

  • #include <stdio.h>
  • int a = 10;  
  • main()
  • {
  •     int i =0
  •     void fun();
  •     fun();            
  •     printf("After First Call");
  •     fun();            
  •     printf("After Second Call");
  •     fun();            
  •     printf("After Third Call");
  • }
  • void fun()
  • {
  •     static int p=0;  
  •     int q = 10;             
  •     printf("value of p %d q %d",p,q);
  •     p=p+10;
  • }

Out Put of Above Program:-

After First Call:-      p= 0, q=10
After Second Call:- p=10, q=10
After Third Call:-     p=20, q=10


No comments:

Post a Comment