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 2 of 4.

Manraj meena said:   1 decade ago
Post decriement and pre decrement operator.

reverse(no--);===>calling processing.

reverse(no);
no=no-1;

So because in that situation before decrement the we called the reverse function in recursive..

Hence the result will be infinite loop.
No chance get decrement.

reverse(--no);==>no=no-1; after that call reverse(no);
So print : 5, 4, 3, 2, 1.

no++ or ++no no effect output infinite.

Anchal said:   1 decade ago
@Manraj.

Can you please tell me why we get same output ie infinite in case of post increment and pre increment?

Anshul said:   1 decade ago
I don't understand why answer is infinite.

Reverse function will repeats itself until no becomes zero.

I think output must be 5, 4, 3, 2, 1.

And when no becomes zero, function will exit from loop.

B&C said:   1 decade ago
Hi all! the prototype of the function has return type int. How can the function be called without returning to some integer?

Sandy said:   1 decade ago
Thanks I totally agree with Manraj Meena for further detail go check concept in C in Depth.

Nee said:   1 decade ago
It is very simple. As we go step by step we will get output as infinite times 5. Since post decrement is the function where the value is first assigned and then gets decremented.

Here, no-- means
no=no-1; ->no=5 is assigned first on "LHS" and then "RHS" no gets decremented. so the final value we get to return is 5 only which is assigned to "LHS". And the same value is again called in reverse() function.

So the same procedure is repeated infinite time.

Hope this will help you

Gopi krishna said:   1 decade ago
HI friends, What happen if(no==0){ return 0; } executes. Control pass to the main or still in the reverse?

Vinushree said:   1 decade ago
How infinte loop?

Priy said:   10 years ago
If suppose it was a pre-decrements than it should 5, 4, 3, 2, 1, 0.

Why zero is not included please explain why we avoid zero?

Sayli said:   9 years ago
Why we avoid 0 in preincreament operator?


Post your comments here:

Your comments will be displayed after verification.