The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+2 votes
2.2k views

GATE2019-27

  1. GATE2019-99
  2. GATE2019-99
  3. GATE2019-99
  4. GATE2019-99

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$
asked in Programming by Veteran (385k points)
edited by | 2.2k views
0
there it should be num --,then it will be correct

4 Answers

+7 votes
Best answer

Basic Points :-

1.after every expression statement  in the for loop there is a sequence point

2. After return value copied, there is a sequence point.

for loop execution :-  for(e1;e2;e3)

on first iteration, expression1 executed ( generally termed as initialization expression. )

next expression2 executed ( generally termed as condition check expression, if it evaluate to non-zero then only, for loop body executed, otherwise for loop terminates.)

after first iteration, expression3 executed  ( generally termed as increment expression. ), after that e2 evaluates and process continues !

 

$for(\color{green}{r()};\color{red}{r()};\color{blue}{r()})$

{

    printf("%d",r());

}

 

before main starts the execution num initialized with 7 ( note that it is stored under static memory due to it is static number. )

on first iteration :- $\color{green}{r()}$ ==> return 7 but num changed to 6.

$\color{red}{r()}$ ==> return 6 but num changed to 5. ==> condition evaluate to true ==> for loop body executes !

  printf("%d",r()); ==> return 5 but num changed to 4. ====> print 5 on the screen.

 

$\color{blue}{r()}$ ==> return 4 but num changed to 3.

$\color{red}{r()}$ ==> return 3 but num changed to 2. ==> condition evaluate to true ==> for loop body executes !

  printf("%d",r()); ==> return 2 but num changed to 1. ====> print 2 on the screen.

 

$\color{blue}{r()}$ ==> return 1 but num changed to 0.

$\color{red}{r()}$ ==> return 0 but num changed to -1. ==> condition evaluate to false ==> for loop over !

 

o/p ::- 52

answered by Veteran (55.8k points)
selected by
+1
why is the ans not 41 i feel it should be 41 because printf will print whatever r() is returning
0
i also got 41
0

@Gate Fever @ check my response below Hope that will help :)

0

@184640 ktr.cse.14

note that, there is one more time r() in the body ! ( which is in black color )

+1 vote

It will print 52

See the function starts from main which has a for loop .

for (r();r();r())

So first r() will return 7 and then 6 will go to second r() which is a condition (why 6 then notice its num-- so first it will return value then decrement , Now second r() which is a condition will return 6 and decrement to 5 ,further as this condition satisfies 5 will go for print :

printf("%d", r());

Now again here there is r() so it will print 5 and decrement again to 4

Which will go to increment/decrement condition of for that is third r() and as we see its a decrement

num--

so it will return 4 and decrement to 3 which will again go to second r() and will return 3 with decrementing to 2 and that 2 will go to print r() which will print 2  .

so final print 52

answered by (255 points)
edited by
0
why is the ans not 41 i feel it should be 41 because printf will print whatever r() is returning
0

 updated my response :)

0 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

answered by (379 points)
edited by
0 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.
answered by Loyal (9.1k points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
47,929 questions
52,326 answers
182,372 comments
67,797 users