GATE | GATE CS 2019 | Question 36

Consider the following C program:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int r(){
 int static num=7;
 return num--;
}
  
int main() {
      
 for(r();r();r()) {
  printf("%d ",r());
  };
    
 return 0;
}

chevron_right


Which one of the following values will be displayed on execution of the programs?
(A) 41
(B) 52
(C) 63
(D) 630


Answer: (B)

Explanation: According to “for” loop in C,

for (initialization expr; test expr; update expr)
{    
     // body of the loop
     // statements we want to execute
}

Code will execute as following flow chart,

Also, note that there is postdecrement (num – –) in given return statement, so it will return previous value of “num” then decrements it by 1.

Static will assign memory once for variable int “num” and all the changes will happen here.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
  
int r(){
 int static num=7;
 return num--;
}
  
int main() {
      
 for(r();r();r()) {
  printf("%d ",r());
  };
    
 return 0;
}

chevron_right


Therefore, given code will print: 5 2.

Option (B) is correct.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.