Didn"t forge 2009-10-29 06:57 采纳率: 25%
浏览 636
已采纳

什么是 c + + 中的"--"运算符?

After reading Hidden Features and Dark Corners of C++/STL on comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4.

Here's the code:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x --> 0) // x goes to 0
    {
        printf("%d ", x);
    }
}

I'd assume this is C, since it works in GCC as well. Where is this defined in the standard, and where has it come from?

转载于:https://stackoverflow.com/questions/1642028/what-is-the-operator-in-c

  • 写回答

21条回答 默认 最新

  • python小菜 2009-12-31 04:17
    关注

    Pay attention

    --> is not an operator. It is in fact two separate operators, -- and >.

    The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

    To better understand, the statement could be written as follows:

    while((x--) > 0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(20条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器