Question:

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

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?

Answer:

That's not an operator -->. That's two separate operators, -- and >.

The condition code is decrementing x, while returning x's original (not decremented) value, and then comparing the original value with 0 using the > operator.

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

while( (x--) > 0 )

相关文章:

  • 2021-06-05
  • 2021-09-15
  • 2021-12-28
  • 2022-02-21
  • 2021-12-25
  • 2022-12-23
  • 2021-12-02
  • 2021-07-04
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-11-17
  • 2022-02-17
  • 2021-09-15
  • 2021-11-12
  • 2022-12-23
相关资源
相似解决方案