Didn"t forge 2009-10-29 06:57 采纳率: 83.3%
浏览 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条)

报告相同问题?