retagged by
27,050 views
59 votes
59 votes

Consider the following C program:

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

Which one of the following values will be displayed on execution of the programs?

  1. $41$
  2. $52$
  3. $63$
  4. $630$
retagged by

9 Answers

2 votes
2 votes

As it is given return num-- ; so the value retuned will be that of num before decreasing it. 

So, in first call, num = 7, returns 7 and becomes 6. Next call finds num = 6, returns 6 and makes num 5.

 

The maechanism of for loop:

     Initialize expression -> Test -> Body -> Update -> Test -> Body -> Update .................continues until test expression is false.

So the values will be :

    Initialize expression (returned 7, num = 6)-> Test (returned 6, num = 5) ->

    Body (returned 5 printed, num = 4  ) ->

    Update (returned 4, num = 3) -> Test (returned 3, num = 2) ->

    Body (returned 2 printed, num = 1) ->

    Update (returned 1, num = 0) -> Test (returned 0 You are terminated, num = -1) .

Values printed 52

edited by
2 votes
2 votes
Call order of r() inside for loop: returned value

initialization : returns 7

condition check: returns 6

printf : returns 5 and prints it

Incr/Dcr: returns 4

condition check: returns 3

printf: returns 2 and prints it

Incr/Dcr: returns 1

Condition check: returns 0

loop breaks

Ans B) 52

Please note that it's post decrement so value is returned first and then decremented.
2 votes
2 votes
ALTERNATE METHOD

The answer could be guessed from the options easily

 for(r();r();r()) ->>>>>> basically we know the second part in the for loop is actual testing condition, to stop the loop, at last, this condition must return 0(zero) so actually we could have done reverse engineering too to cross-check, in case we miss out something.

Also we know with each call to r()  num decreases, therefore while backtracking we will increase the value for each subsequent call to function.

Therefore the last call to r() returns 0 (testing point in loop i.e. for( ; r(); ))

Second last call to r() returns 1 (Incrementing or decrementing place in loop i.e. for( ; ; r())

third last call to function r() that actually got printed should return 2,

similarly, if we backtrack we can get the first number printed was 5

So even with the basic principles, we could guess the right answer :D
Answer:

Related questions

21 votes
21 votes
3 answers
1
18 votes
18 votes
2 answers
2
Arjun asked Feb 7, 2019
15,357 views
Consider the following C program:#include <stdio.h int main() { int a[] = {2, 4, 6, 8, 10}; int i, sum=0, *b=a+4; for (i=0; i<5; i++) sum=sum+(*b-i)-*(b-i); printf("%d\n"...
14 votes
14 votes
6 answers
3
Arjun asked Feb 7, 2019
13,417 views
Consider the following C program:#include <stdio.h int main() { int arr[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 5}, *ip=arr+4; printf(“%d\n”, ip ); return 0; }The numb...
13 votes
13 votes
4 answers
4
Arjun asked Feb 7, 2019
9,567 views
Consider the following C program :#include<stdio.h int jumble(int x, int y){ x = 2*x+y; return x; } int main(){ int x=2, y=5; y=jumble(y,x); x=jumble(y,x); printf("%d \n"...