4,440 views
1 votes
1 votes

#include <stdio.h>

int fun()

{

  static int num = 16;

  return num--;

}

int main()

{

  for(fun(); fun(); fun())

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

  return 0;

}

Predict the output along with the explanation?

3 Answers

Best answer
7 votes
7 votes

Here we need to keep in mind three things :

a) The initialisation statement of for loop runs only once while the other two runs till loop fails

b) Every function has its static area..So once a static variable is initialised in a function ; it is not re-initialised i.e. in next function call the value is taken from previous invocation of that function. 

c) Post - increment/decrement operator uses the value then increments / decrements the concerned variable.

So here ,

Initial value of   num  = 16

So using initialisation step of for loop which uses a fun() call , it will return 16 as value since in "num--" first num value is used n then decremented.

Now num value  =  15 after initialisation..Now when condition part comes , it just checks the return value of fun which will be 15 and then num is decremented to 14. Now we come to body of loop which again uses fun() call in printf() function..Here fun() will return 14 which in and then decrement it to 13..As the return value here was 14 , it is printed by printf().

Now third statement of for loop will work which uses fun() as well..It will return 13 and decrement num to 12..

Having iterated for the first time , now initialisation will not be executed as it is executed only in 1st iteration..Hence condition part evaluates num to 12 and decrements it to 11 according to function definition of fun()..

Now in the body of the loop , fun() is used again which returns value of 11 and hence 11 is printed as fun() is used as an argument for printf() function.So in the second iteration we get the printed value as "11"..

Likewise in the subsequent iterations :  8 , 5 and 2 are printed .

So when 2 is printed after that in the third statement of for loop num value is 1 which is returned by the third statement of the for loop and then num becomes 0..Then in the next loop 0 is returned and num becomes -1..But as return value is 0 hence condition evaluates to false and the loop terminates.

Hence the output sequence is :  14 , 11 , 8 , 5 , 2

selected by
0 votes
0 votes
The type of decrement in the function which is already defined is post decrement which means first value is decremented later.The values returned by fun() would be 16(initialization),15(condition),14(print statement),13(decrement condition in for loop),12(condition),11(print),10(decrement condition in for loop),9(condition),8(print statement),7(decrement condition in for loop),6(condition),5(print statement),4(decrement condition in for loop),3(condition)2(print statement),1(decrement condition in for loop),0(condition)

the values that are printed would be  14,11,8,5,2
0 votes
0 votes
working of for loop:

1. initialisation 2 . check condition 3. run the code inside loop

So according to it fun() is called 3 times check the return first fun() return: 16 then decrement

2nd fun() call returns 15 and then decrements and finally 3rd fun() call returns 14 and then decrements

and answer will continue in this way 14,11,8....

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
47 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
3
Debargha Mitra Roy asked Apr 10
103 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error
1 votes
1 votes
1 answer
4
SSR17 asked Feb 29
252 views
#include <stdio.h int main() { int i = -1; int x = (unsigned char)i; printf("%d", x); return 0; }output is 255 , but please explain how