在 comp.lang.c++.moderated
上阅读 C++/STL 的隐藏功能和 暗角后,我完全惊讶于以下代码片段在 Visual Studio 2008 和 G++ 4.4 中编译和工作。
这是代码:
#include <stdio.h>
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}
输出:
9 8 7 6 5 4 3 2 1 0
我假设这是 C,因为它也适用于 GCC。这在标准中是哪里定义的,它是从哪里来的?
原文由 GManNickG 发布,翻译遵循 CC BY-SA 4.0 许可协议
-->
不是操作员。它实际上是两个独立的运算符,--
和>
。The conditional’s code decrements
x
, while returningx
’s original (not decremented) value, and then compares the original value with0
using the>
运营商。为了更好地理解,该语句可以写成如下: