The Gateway to Computer Science Excellence
+12 votes

Consider the following C program:

#include <stdio.h>
int r() {
       static int num=7;
       return num--;
int main() {
       for (r();r();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$
in Programming by Veteran (431k points)
edited by | 4.5k views
there it should be num --,then it will be correct

9 Answers

+29 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 !





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()}$ $\implies$ return $7$ but num changed to $6$.

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

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

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

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

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

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

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

Hence option $B$ : $52$

by Veteran (65.6k points)
edited by
why is the ans not 41 i feel it should be 41 because printf will print whatever r() is returning
i also got 41

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


@184640 ktr.cse.14

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



here return num-- means return num & then num-- ???

read the 2nd basic point reference for that is " sequence points in C " from wiki
It is post decremented

@Shaik Masthan

Plz check this point

$\color{red}{r()}$ $\implies$ return $6$ but num changed to $5$.



printf("%d", $\color{orange}{r()}$);

So, $\color{orange}{r()}$ will be executed before printing.

But inside $\color{orange}{r()}$ there is post decrement for num, i.e. first assign then decrement operation.

Here no variable to assign . So, printing will be done before decrement operation and 5 will print.

Am I right?

Why r u asking for sequence point. Here I think, no need of that

@Shaik Masthan

check this point too

+6 votes

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


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

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

 updated my response :)

+4 votes
52. It will be decremented only after value is sent
by Active (1.2k points)
No it will print 41.
What is the answer for convert function.
I checked it on ideone. It is 52
initialization, comparison



and so on
+3 votes
by (41 points)
+3 votes

May be this works : 

Must know the property of loop that :

(i) for first run initialization part run for first and last time 

(2) condition part runs each time.

(3) updation part runs after one entry in code

(4) In last run where condition fails still for statement part run i.e. condition part fails surely no entry in code part but updation part runs.

by Junior (615 points)
+1 vote

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

by Active (1.1k 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.
by Boss (10.5k points)
0 votes

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
by (67 points)

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
50,737 questions
57,280 answers
104,841 users