阅读后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,因为它在海合会也是工作。标准中的定义在哪里,它来自何处?


当前回答

xC++的反方向可以更快地达到零:

int x = 10;

while( 0 <---- x )
{
   printf("%d ", x);
}

8 6 4 2

你可以用箭来控制速度!

int x = 100;

while( 0 <-------------------- x )
{
   printf("%d ", x);
}

90 80 70 60 50 40 30 20 10

;)

其他回答

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

#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            

-->. x设置后,即检查条件后降值。x>0 ?.

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

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
    }
}

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

为什么这么复杂?

对最初问题的简单答案是:

#include <stdio.h>

int main()
{
    int x = 10;
    while (x > 0)
    {
        printf("%d ", x);
        x = x-1;
    }
}

我并不是说你们应该这样做,但是它也这样做,而且会用一个文章回答问题。

缩略x--以上所述,只是一个直截了当的速记,>只是普通的比普通的要大operator没有大谜!

) 太多人让简单的事情变得复杂,

这是

#include <stdio.h>

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

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