阅读后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
    }
}

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

其他回答

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

#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            

这是

#include <stdio.h>

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

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

(x --> 0)中指(x-- > 0).

  1. 您可以使用(x -->)
    Output: 9 8 7 6 5 4 3 2 1 0
  1. 您可以使用(-- x > 0)意思是(--x > 0)
    Output: 9 8 7 6 5 4 3 2 1
  1. 您可以使用
(--\
    \
     x > 0)

Output: 9 8 7 6 5 4 3 2 1

  1. 您可以使用
(\
  \
   x --> 0)

Output: 9 8 7 6 5 4 3 2 1 0

  1. 您可以使用
(\
  \
   x --> 0
          \
           \
            )

Output: 9 8 7 6 5 4 3 2 1 0

  1. 您也可以使用
(
 x 
  --> 
      0
       )

Output: 9 8 7 6 5 4 3 2 1 0

同样,您也可以尝试很多方法来成功执行此命令 。

使用-->降幅曾经(而且在某些情况下仍然)比x86结构的递增速度快。-->建议x将会0,并吸引有数学背景的人。

丑陋的怪胎, 但我会使用这个:

#define as ;while

int main(int argc, char* argv[])
{
    int n = atoi(argv[1]);
    do printf("n is %d\n", n) as ( n --> 0);
    return 0;
}