阅读后C++/STL 的隐藏特征和暗角上 年 月comp.lang.c++.moderated我感到非常惊讶的是, 以下的片段在2008年视觉工作室和G++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,因为它在海合会也是工作。标准中的定义在哪里,它来自何处?


当前回答

传统的方式,我们定义状态,而循环括号"()"和牙套内的终止条件"{}",但这个-- & >是一个同时定义全部的方法。例如:

int abc(){
    int a = 5
    while((a--) > 0){ // Decrement and comparison both at once
        // Code
    }
}

上面写着,降级a运行循环直到时间a大于0

换句话说,它本该是这样的:

int abc() {
    int a = 5;
    while(a > 0) {
        a = a -1 // Decrement inside loop
        // Code
    }
}

无论是哪种方式,我们都做同样的事情,实现同样的目标。

其他回答

while( x-- > 0 )

这是如何解析的。

这是

#include <stdio.h>

int main(void) {
  int x = 10;
  while (x-- > 0) { // x goes to 0
    printf("%d ", x);
  }
  return 0;
}

空间让事情变得有趣--和 年 年 年 年 和 年 年>比较。

和这个完全一样

while (x--)

这是两个操作员的组合 第一--用于减少值,并且>用于检查值是否大于右手操作。

#include<stdio.h>

int main()
{
    int x = 10;

    while (x-- > 0)
        printf("%d ",x);

    return 0;
}

产出将是:

9 8 7 6 5 4 3 2 1 0            

--衰减操作员和操作员>大于运算符 。

两个操作员是作为一个单一操作员来操作的。-->.