C Programming - Functions - Discussion

Discussion Forum : Functions - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
int reverse(int);

int main()
{
    int no=5;
    reverse(no);
    return 0;
}
int reverse(int no)
{
    if(no == 0)
        return 0;
    else
        printf("%d,", no);
    reverse (no--);
}
Print 5, 4, 3, 2, 1
Print 1, 2, 3, 4, 5
Print 5, 4, 3, 2, 1, 0
Infinite loop
Answer: Option
Explanation:

Step 1: int no=5; The variable no is declared as integer type and initialized to 5.

Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter.

The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);.

The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5) infinitely.

Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1. Because before calling the function, it decrements the value of 'n'.

Discussion:
34 comments Page 1 of 4.

Samata said:   2 decades ago
It is decrement operator then plz tell more explation about that.

Saket said:   1 decade ago
It doesn't matter whatever maybe the operator is. Because it will execute the function first & then it will increment/decrement the value. But function ends at this increment/decrement operators & executing same function again so there is no use of these operators over here.

Laxmi said:   1 decade ago
Yes Saket I agree with you. Actually there is not any false condition so it will execute infinitely.

Hairry said:   1 decade ago
In this program reverse()is a recursive function and according to all of U we can't use post-increment or decrement operator in recursive functions.

Is it right ?. If Yes/No then why?

Arun said:   1 decade ago
Yes its correct.

Bhavna said:   1 decade ago
As far as I knw, post decrement uses the value as it is, n in next step it decrements it, so I think ans is wrong, as it will print 5, 5, 4, 3, 2, 1. ?

Bijo said:   1 decade ago
Yes I agree with bhavana I don't understand how for a post increment the value just is the same it has to be decrement after 1st initialization isn't it?

Yash said:   1 decade ago
@ Bhavna & Bijo

I also agree with both of you.

Is the answer is correct ?
If yes then please explain it.

Mehar said:   1 decade ago
Hi. The answer is infinite times 5 w'll be printed as it is recursive call, it keeps calling with the value 5, as it s post incrementing. You can execute it 4 confirmation. The answer is D.

Atul said:   1 decade ago
In this case,

When reverse (no) is called recursively, it will every time get the value as 5 and not the decremented value as 4, 3, 2 and so on. This is because every time reverse (no) is called, the variable named "no" is a new different variable not the same. The decremented value is in the previous function call (or stack frame) and therefore after calling the reverse (no) we are entering a new stack frame resulting into a new variable "no" which is having a value 5 every time. Hence the result will be infinite loop.

Hope this helped.


Post your comments here:

Your comments will be displayed after verification.