Firstly we can see there is static keyword while declaration of i, so memory to variable i is given at compile time in static area. Also, static memory is very small memory hence it is cleared, and no garbage resides there and hence if any variable is declared in it and not initialized then by default value of that variable is 0.
So in static area one variable i of int type is created at compile time and its value is 0 as of now.
Now when program will run, main() will be pushed in stack.
Now inside stack one variable of int type is declared and initialized with 0.
then for loop runs
for i=0 it will call display() and inside display as soon as you go processor will see static int i and it will think that “since static keyword is used hence memory is allocated to this variable i at compile time only” hence processor will skip this line and go to next statement that is if(i<=printf("GATE24")).
the i that will be used in display() is the i that is declared in static part.
now printf will print the string "GATE24" {printed 1st time} as well as it also return the number of characters that it printed so 6 will be returned and compared to i
so 0<=6 is true hence block of if() will run and it will make i= i+2; so now value of i at static memory is 2
then again display() is called now also processor will skip first line of display() because static keyword is used. and again if(i<=printf("GATE24")) will be checked and "GATE24"{printed 2nd time} is printed and 6 is again returned by printf.
since 2<=6 so again block of if() will run and
i=i+2 so i=4 now
again display() called and again "GATE24" {printed 3rd time} is printed and 6 is again returned by printf.
since 4<=6 so again block of if() will run and
i=i+2 so i=6 now
again display() called and again "GATE24"{printed 4th time} is printed and 6 is again returned by printf.
since 6<=6 so again block of if() will run and
i=i+2 so i=8 now
again display() called and again "GATE24"{printed 5th time} is printed and 6 is again returned by printf.
since 8<=6 is false so block of if() will not run and control will come to main().
now in main() for loop will increment i of stack and it will become 1 and 1<3 so again display called so again if if(i<=printf("GATE24")) run and "GATE24"{printed 6th time} is printed and 6 is again returned by printf. but since i in display has value 8 so if condition fails so again control go to main .
now inside main i =2 and 2>3 so again display called and again GATE24"{printed 7th time} is printed and 6 is again returned by printf. and if fails since 8<=6 so again go to main() but now in main i=3 and 3<3 fails so control comes out of for loop.
now in main next statement is return so main() is popped out of stack and then static memory allocated to i is also deallocated and program over.
so totally GATE24" is printed for 7 times.
in this problem the catch is that firsly printf() runs then condition checked.
i hope my solution is correct. thank you.