What does static variable means?
Static variable is a variable which retain its value between the function calls.
They are initialized only once and their scope is within the function where they are defined.
A static variable inside a function has a lifespan as long as program runs. It won't be allocated every time function is called and deallocated when function returns.
#include<stdio.h>
void function ()
{
int a=10;
static int sa=10;
a+=5;
sa+=5;
print("a=%d sa=%d",a,sa);
}
void main()
{
int i;
for(i=0;i=<3;i++)
function ();
}
Output:
sa:15 a:15
sa:20 a:15
sa:25 a:15
sa:30 a:15
Reference:
1)https://stackoverflow.com/questions/5033627/static-variable-inside-of-a-function-in-c
2)https://youtu.be/VGovIzt-ggM
No comments:
Post a Comment