Saturday, 31 December 2016

Breif description about static storage class


The static storage class is very important in C language so i'm going to discuss it in brief 
just have a glance below and i have compared it with an example::
The features of static storage class are as following:
StorageMemory
Keywordstatic
Default initial valueZero
ScopeLocal to the block, in which the variable is defined
LifeValue of the variable persists between different function calls.
#include<stdio.h>
void add();
int main()
{
add();
add();
add();
add();
return 0;
}
void add()
{
 static int i=1;
 printf("\n%d",i);
 i=i+1;
}
OUTPUT : 1 2 3

 static variable do not disappear when the function is no longer active. There value persist. If control comes back to the same function again , the static variables have the same values they had last time around.
Note:
if the storage class is static, then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.

No comments:

Post a Comment