• support@answerspoint.com

What is the name of the --> operator?

1897

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

Here's the code:

#include 
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?

  • C

  • C++

  • asked 8 years ago
  • B Butts

2Answer


0

--> 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 )
  • answered 8 years ago
  • Sunny Solu

0
#include <stdio.h>
int main(void)
{
     int x = 10;

     while( x-- > 0 ) // x goes to 0
     {
       printf("%d ", x);
     }

     return 0;
}

Just the space make the things look funny, -- decrements and > compares.

  • answered 8 years ago
  • Gul Hafiz

Your Answer

    Facebook Share        
       
  • asked 8 years ago
  • viewed 1897 times
  • active 7 years ago

Best Rated Questions