in DS
525 views
0 votes
0 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?

 

41

52

63

630
in DS
525 views

4 Comments

@ankit3009 that's good. even though you're looking atmost 40 questions, reading the discussion makes you better understanding the questions.

1
1
Okay thank you Sir :)
1
1

Great strategy @ankit3009 

1
1

1 Answer

0 votes
0 votes

52

For loop has following order of execution :

  1. Initialization 
  2. Condition evaluation 
  3. Execute the body if condition is true, else exit
  4. Updation
  5. Repeat steps 2 to 4 until the loop terminates.

On every step from 1 to 4, function r() is being called, which decrements the value of static variable and returns the updated value. But we are using post decrement unary operator. Hence, the value will be returned first and then updated. 

2 Comments

Yes.Correct.
1
1
just like while loop execution.
0
0

Related questions

0 votes
0 votes
1 answer
1
rsansiya111 asked in DS Dec 19, 2021
276 views
rsansiya111 asked in DS Dec 19, 2021
276 views
0 votes
0 votes
1 answer
2
rsansiya111 asked in DS Dec 17, 2021
304 views
rsansiya111 asked in DS Dec 17, 2021
304 views
0 votes
0 votes
1 answer
3
rsansiya111 asked in DS Dec 16, 2021
374 views
rsansiya111 asked in DS Dec 16, 2021
374 views
0 votes
0 votes
1 answer
4
rsansiya111 asked in DS Dec 16, 2021
333 views
rsansiya111 asked in DS Dec 16, 2021
333 views