in Programming in C
10,092 views
3 votes
3 votes
  1. #include <stdio.h>
  2.     void reverse(int i);
  3.     int main()
  4.     {
  5.         reverse(1);
  6.     }
  7.     void reverse(int i)
  8.     {
  9.         if (i > 5)
  10.             return ;
  11.         printf("%d ", i);
  12.         return reverse((i++, i));
  13.     }

I am not able to understand how to solve this "return reverse((i++, i));" statement. 

in Programming in C
10.1k views

4 Comments

which parameter will be passed to above recursive call??? 

return reverse((i++, i));
0
0

No it is not an error, It is comma operator. It takes the right most value and assigns that to the caller

For eg:

int a = (10, 20, 30)

So, a will be assigned 30

In the question

reverse((i++, i))

i++ would happen, and then the value of second i would be returned, which is an incremented value.

so the incremented 'i' would be passed to reverse(..)

1
1
yeah :p
0
0

2 Answers

1 vote
1 vote
Best answer

hope it helps...

selected by

1 comment

In the return function why you are passing return (1,2). I think return (1.1) should be called because post increment operator increments the value after termination of that statement and that statement will be terminated only after return call is performed. Please correct me if I am wrong
0
0
4 votes
4 votes

I checked it, and cout<<(a,b) simply takes the second value, same with functions.  

if we are passing (a,b) simply b will be taken as the argument, not a. 

An expression that looks like this

(a, b)

is a comma expression, its value is its result is its rightmost operand, i.e.

(a, b, ..., z)

would produce the value of z.

by