[英]The following c code gives output 1 2 3 4 5 . How the code is executed?
I am not getting that how the following code is executing and output is 1 2 3 4 5 . 我没有得到以下代码如何执行和输出是1 2 3 4 5。 Specially that return statement in reverse function with (i++, i).
特别是带有(i ++,i)的反向函数的return语句。
#include <stdio.h>
void reverse(int i);
int main()
{
reverse(1);
}
void reverse(int i)
{
if (i > 5)
return ;
printf("%d ", i);
return reverse((i++, i));
}
The expression (i++, i)
uses the comma operator -> it first evaluates the left hand side and then the right hand side, the result is that of the right hand side. 表达式
(i++, i)
使用逗号运算符 - >它首先计算左侧,然后是右侧,结果是右侧的结果。 As this operator also introduces a sequence point, the result is well-defined: it's the value of i
after incrementing it. 由于此运算符还引入了一个序列点,因此结果是明确定义的:它是递增后的
i
的值。
In this example, it's just needlessly confusing code because it has the exact same result as just writing reverse(++i);
在这个例子中,它只是不必要地混淆代码,因为它与只写
reverse(++i);
具有完全相同的结果reverse(++i);
(or even reverse(i + 1);
which would better match a functional/recursive approach). (甚至
reverse(i + 1);
这将更好地匹配功能/递归方法)。
As it was already rightly pointed out by Felix, it is using the comma operator. 正如Felix已正确指出的那样,它正在使用逗号运算符。 You can simply write reverse(i++) or reverse(++i) or reverse(i+1) that'll do!
你可以简单地写反向(i ++)或反向(++ i)或反向(i + 1)那样做!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.