in Programming in C retagged by
578 views
1 vote
1 vote

I am getting (b) 5 ?? Can you please explain the_ int i_ in for loop and display()

[MCQ]
44. Consider the following function:
void display()
{
 static int i;
 if(i<=printf("GATE24"))
{
 i=i+2;
 display();
 }
} 
int main()
{
 int i=0;
 for(i=0;i<3;i++)
 display();
 return 0;
}
The number of times printf() executed is-
(a) 6 (b) 5
(c) 7 (d) 9
in Programming in C retagged by
by
578 views

1 comment

This is lil bit tricky and really good question
0
0

2 Answers

3 votes
3 votes
Best answer

 

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.

 

 

 

edited by
0 votes
0 votes

Here's a concise explanation:-

  1. 1.The display() function is called recursively within itself, with a static variable i initialized to 0.
  2. 2.When display() is called, it prints "GATE24" and increments i by 2 if the condition i <= printf("GATE24") is true.
  3. 3.The condition compares the current value of i (from the static memory) with the number of characters printed by printf().
  4. 4.The printf() function is executed before the condition check.
  5. 5.The recursion stops when the condition becomes false.
  6. 6.After the display() calls in main(), the final value of i is 8.
  7. 7.During each call to display(), "GATE24" is printed, contributing to a total of 7 prints.