int a,b; /* Here a,b are global variable whose inital value is 0 for both a,b*/
void f();
main()
{
static int a =1; /*static keyword is one time initialization, use many time*/
f();
a*=2; /* line no 7 is same as a=a*2*/
f();
printf("%d%d",a,b); /* printf function print the value a,b in main scope but here in main function b is not define so it will take value from global variable b */
}
void f()
{
static int a =2; /*same use as line no 5 */
int b =2 /* here b is define in the f function scope, it's not static so every time when b is initialized, it will assigned as b = 2 */
a*=b++; /* line no 15 can be written as a = a*b++ */
printf("%d%d",a,b); /* this printf print the values of a,b which is define in function f scope */
}
execution of program is as follow:
Line no 1 is executed first and assign the value 0 to both GV a,b. After that main function is executed.
In main function a is static variable assign the value 1, then call the function f().
In function F() a is static variable & b is normal variable, both assign the value 2. Line no 15 use 2 operator * and ++(post-increment). Here post-increment has high priority than * and associativity is R2L. This gives the value of a,b as 4,3.
after that comes to the main function & assigned the new value of a as 2, again call function f() and do the same things. static var a use current value 4 and variable b again initialized as 2, again execute line no 15 which gives the value of a,b as 8,3.
returning to the main function, printf function print a & b value, here b is not defined so it used the value of GV as 0 so printf gives 2,0.
so values of a & b are respectively according to their printf statements: