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.

Monier said:   2 years ago
Infinite loop is correct but why ?

First, we should know that the no in main is not the same as the function reverse because you pass the value to the function not refrence.

Second, when you call a function it opens the location in the stack and if you call the same function again it will open another location so if you make a decrement to (no) in the first call the second call will not know you decreased it to 4 ##well the (no) in first will save the value of new (no) by 4 ## it will work with a parameter which it will be 5 all times.

If you call the function with refrence by pointer for example the porgram will print 5 4 3 2 1.

Likewise if used pre-decrement.

Hope this helped.
(1)

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.

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

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.

Chetan said:   8 years ago
@Mohit Thaper & @Tushar.

D is correct answer.

As no is 5. So it is transferred to function, where it does not equal to 0. Then control goes to else part and prints 5. After that post operator used. Hence, it simply transfers same value i.e. 5 and again same process continue. It continuously produces 5.
(1)

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.

Shubham said:   7 years ago
It continously print 5.

Because the characteristic of post decrement is that it gets implement after all operation executed so it fist call recusively then it decrement the no but now again reverse function gets 5 as a fresh no and tgis process continues.

Tanmay said:   6 years ago
I think A is correct because;
5==0 // false it comes to else
Print 5
Reverse(no--) // now no = 4
Similarly, at last no=1
Reverse (no--) no = 0
If 0==0 // true
Return 0;

After return it comes to main function.
So, option A is correct.
(1)

Amrendra said:   8 years ago
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. And print 5 5 5 5 to infinity.

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.


Post your comments here:

Your comments will be displayed after verification.