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:
Storage | Memory |
Keyword | static |
Default initial value | Zero |
Scope | Local to the block, in which the variable is defined |
Life | Value 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