retagged by
27,036 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

Best answer
93 votes
93 votes

Basic Points:-

  1. After every expression statement  in the for loop, there is a sequence point
  2. After the return value is copied, there is a sequence point.

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

On the first iteration, expression$1$ executed ( generally termed as initialization expression,)

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

After the first iteration, expression$3$ is executed  ( generally termed as increment expression. ), after that $e2$ evaluates and the process continues!

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

{

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

}

before the main starts, the execution num is initialized with $7$ ( note that it is stored under static memory due to it is a 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$

edited by
12 votes
12 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.

7 votes
7 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

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

edited by
5 votes
5 votes
52. It will be decremented only after value is sent
Answer:

Related questions

21 votes
21 votes
3 answers
1
18 votes
18 votes
2 answers
2
Arjun asked Feb 7, 2019
15,348 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,408 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,562 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"...