524 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

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. 

Related questions

0 votes
0 votes
1 answer
1
rsansiya111 asked Dec 19, 2021
287 views
0 votes
0 votes
1 answer
2
rsansiya111 asked Dec 17, 2021
316 views
0 votes
0 votes
1 answer
3
rsansiya111 asked Dec 16, 2021
385 views
0 votes
0 votes
1 answer
4
rsansiya111 asked Dec 16, 2021
337 views