C Programming - Functions - Discussion


What will be the output of the program?

int reverse(int);

int main()
    int no=5;
    return 0;
int reverse(int no)
    if(no == 0)
        return 0;
        printf("%d,", no);
    reverse (no--);

[A]. Print 5, 4, 3, 2, 1
[B]. Print 1, 2, 3, 4, 5
[C]. Print 5, 4, 3, 2, 1, 0
[D]. Infinite loop

Answer: Option D


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'.

Samata said: (Jul 24, 2010)  
It is decrement operator then plz tell more explation about that.

Saket said: (Sep 30, 2010)  
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: (Apr 4, 2011)  
Yes Saket I agree with you. Actually there is not any false condition so it will execute infinitely.

Hairry said: (May 11, 2011)  
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: (Aug 6, 2011)  
Yes its correct.

Bhavna said: (Sep 1, 2011)  
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: (Oct 3, 2011)  
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: (Dec 6, 2011)  
@ Bhavna & Bijo

I also agree with both of you.

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

Mehar said: (Dec 21, 2011)  
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: (May 28, 2013)  
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.

Manraj Meena said: (Jun 10, 2013)  
Post decriement and pre decrement operator.

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


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: (Jun 24, 2013)  

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

Anshul said: (Jul 7, 2013)  
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&Amp;C said: (Jul 25, 2013)  
Hi all! the prototype of the function has return type int. How can the function be called without returning to some integer?

Sandy said: (Oct 24, 2013)  
Thanks I totally agree with Manraj Meena for further detail go check concept in C in Depth.

Nee said: (Nov 7, 2013)  
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: (Sep 9, 2014)  
HI friends, What happen if(no==0){ return 0; } executes. Control pass to the main or still in the reverse?

Vinushree said: (Jul 10, 2015)  
How infinte loop?

Priy said: (Dec 2, 2015)  
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: (Jul 17, 2016)  
Why we avoid 0 in preincreament operator?

Saurabh Gupta said: (Nov 10, 2016)  
I agree with you @@Bhavna. The output should be 5,5,4,4,3, 3,2,2,1,1.

Subasri said: (Jun 21, 2017)  
void myfunc(int x){
int main()
return 0;

output: 001234

Anyone, please explain this.

Amrendra said: (Jun 25, 2017)  
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.

Savita said: (Aug 10, 2017)  
Thanks for your explanation @Atul.

Mohit Thapar said: (Sep 5, 2017)  
The reverse is a function with return type int. Hence it will produce an error.

Tushar said: (Nov 3, 2017)  
How we will know that function is 'recursive' ?

Chetan said: (Dec 6, 2017)  
@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.

Shubham said: (Aug 15, 2018)  
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.

Akshatha Patil said: (Jul 10, 2019)  
Thanks @Manraj.

Swetha said: (Aug 22, 2019)  
When the function is repeating continuously why can't the stack overflow?

How it becomes infinite?

Tanmay said: (Aug 25, 2019)  
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.

Xiyaz said: (Sep 17, 2020)  
Nice explanation, thanks @Chetan.

Mangesh Kulkarni said: (Jun 25, 2021)  
In Increment/Decrement Operator following path is Taken:

1.Pre-increment/pre decrement.
2.Substitute value.
4.Post Increment/post decrement.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.