I got this question from a website asking to give the output of the code
void reverse(int i)
{
if (i > 5)
return ;
printf("%d ", i);
return reverse((i++, i));
}
int main(int argc, char *argv[]) {
reverse(1);
return 0;
}
Output is 1 2 3 4 5
but reverse
function is called recursively passing two values within parantheses. How precedence and associativity working here?
,
in (i++, i)
is a comma operator. It's operands evaluate from left to right. It evaluates i++
, value of i
get incremented and the value of the expression i++
is discarded and then the incremented value is passed to the function. So, ultimately only a single argument is passed to the function reverse
.