Static Variables in C.
#include <stdio.h>
int b = 34;
// this is a global variable since it is declared outside the main
int func1(int b1)
{
static int myvar = 98;
printf("The value of myvar is %d\n", myvar);
myvar++;
printf("The value of inside func1 is %d\n", b);
printf("The address of inside func1 is %d\n", &b);
return b1 + myvar;
}
int main()
{
int b1 = 344;
printf("The address of inside main is %d\n", &b);
int val = func1(b);
val = func1(b);
val = func1(b);
val = func1(b);
val = func1(b);
val = func1(b);
int *ptr = &val;
printf("The value of func1 is %d\n", val);
return 0;
}
OUTPUT:
The address of inside func1 is 4210692
The value of myvar is 102
The value of inside func1 is 34
The address of inside func1 is 4210692
The value of myvar is 103
The value of inside func1 is 34
The address of inside func1 is 4210692
The value of func1 is 138
Comments
Post a Comment