What is the "-->" operator in C/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); } } Output: 9 8 7 6 5 4 3 2 1 0 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?

--&gt; is not an operator. It is in fact two separate operators, -- and &gt;. The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the &gt; operator. To better understand, the statement could be written as follows: while( (x--) &gt; 0 )

Or for something completely different... x slides to 0. while (x --\ \ \ \ &gt; 0) printf("%d ", x); Not so mathematical, but... every picture paints a thousand words...

That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard. Joking aside, they are two different operators: -- and &gt; described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.

x can go to zero even faster in the opposite direction: int x = 10; while( 0 &lt;---- x ) { printf("%d ", x); } 8 6 4 2 You can control speed with an arrow! int x = 100; while( 0 &lt;-------------------- x ) { printf("%d ", x); } 90 80 70 60 50 40 30 20 10 ;)

It's equivalent to while (x-- &gt; 0) x-- (post decrement) is equivalent to x = x-1 so, the code transforms to: while(x &gt; 0) { x = x-1; // logic } x--; // The post decrement done when x &lt;= 0

It's #include &lt;stdio.h&gt; int main(void) { int x = 10; while (x-- &gt; 0) { // x goes to 0 printf("%d ", x); } return 0; } Just the space makes the things look funny, -- decrements and &gt; compares.

The usage of --&gt; has historical relevance. Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture. Using --&gt; suggests that x is going to 0, and appeals to those with mathematical backgrounds.

Utterly geek, but I will be using this: #define as ;while int main(int argc, char* argv[]) { int n = atoi(argv[1]); do printf("n is %d\n", n) as ( n --&gt; 0); return 0; }

while( x-- &gt; 0 ) is how that's parsed.

One book I read (I don't remember correctly which book) stated: Compilers try to parse expressions to the biggest token by using the left right rule. In this case, the expression: x--&gt;0 Parses to biggest tokens: token 1: x token 2: -- token 3: &gt; token 4: 0 conclude: x-- &gt; 0 The same rule applies to this expression: a-----b After parse: token 1: a token 2: -- token 3: -- token 4: - token 5: b conclude: (a--)-- - b I hope this helps to understand the complicated expression ^^

This is exactly the same as while (x--)

Anyway, we have a "goes to" operator now. "--&gt;" is easy to be remembered as a direction, and "while x goes to zero" is meaning-straight. Furthermore, it is a little more efficient than "for (x = 10; x &gt; 0; x --)" on some platforms.

This code first compares x and 0 and then decrements x. (Also said in the first answer: You're post-decrementing x and then comparing x and 0 with the &gt; operator.) See the output of this code: 9 8 7 6 5 4 3 2 1 0 We now first compare and then decrement by seeing 0 in the output. If we want to first decrement and then compare, use this code: #include &lt;stdio.h&gt; int main(void) { int x = 10; while( --x&gt; 0 ) // x goes to 0 { printf("%d ", x); } return 0; } That output is: 9 8 7 6 5 4 3 2 1

My compiler will print out 9876543210 when I run this code. #include &lt;iostream&gt; int main() { int x = 10; while( x --&gt; 0 ) // x goes to 0 { std::cout &lt;&lt; x; } } As expected. The while( x-- &gt; 0 ) actually means while( x &gt; 0). The x-- post decrements x. while( x &gt; 0 ) { x--; std::cout &lt;&lt; x; } is a different way of writing the same thing. It is nice that the original looks like "while x goes to 0" though.

There is a space missing between -- and &gt;. x is post decremented, that is, decremented after checking the condition x&gt;0 ?.

-- is the decrement operator and &gt; is the greater-than operator. The two operators are applied as a single one like --&gt;.

It's a combination of two operators. First -- is for decrementing the value, and &gt; is for checking whether the value is greater than the right-hand operand. #include&lt;stdio.h&gt; int main() { int x = 10; while (x-- &gt; 0) printf("%d ",x); return 0; } The output will be: 9 8 7 6 5 4 3 2 1 0

Actually, x is post-decrementing and with that condition is being checked. It's not --&gt;, it's (x--) &gt; 0 Note: value of x is changed after the condition is checked, because it post-decrementing. Some similar cases can also occur, for example: --&gt; x--&gt;0 ++&gt; x++&gt;0 --&gt;= x--&gt;=0 ++&gt;= x++&gt;=0

C and C++ obey the "maximum munch" rule. The same way a---b is translated to (a--) - b, in your case x--&gt;0 translates to (x--)&gt;0. What the rule says essentially is that going left to right, expressions are formed by taking the maximum of characters which will form an valid expression.

Why all the complication? The simple answer to the original question is just: #include &lt;stdio.h&gt; int main() { int x = 10; while (x &gt; 0) { printf("%d ", x); x = x-1; } } It does the same thing. I am not saying you should do it like this, but it does the same thing and would have answered the question in one post. The x-- is just shorthand for the above, and &gt; is just a normal greater-than operator. No big mystery! There are too many people making simple things complicated nowadays ;)

Conventional way we define condition in while loop parenthesis"()" and terminating condition inside the braces"{}", but this -- &amp; &gt; is a way one defines all at once. For example: int abc(){ int a = 5 while((a--) &gt; 0){ // Decrement and comparison both at once // Code } } It says, decrement a and run the loop till the time a is greater than 0 Other way it should have been like: int abc() { int a = 5; while(a &gt; 0) { a = a -1 // Decrement inside loop // Code } } Both ways, we do the same thing and achieve the same goals.

(x --&gt; 0) means (x-- &gt; 0). You can use (x --&gt;) Output: 9 8 7 6 5 4 3 2 1 0 You can use (-- x &gt; 0) It's mean (--x &gt; 0) Output: 9 8 7 6 5 4 3 2 1 You can use (--\ \ x &gt; 0) Output: 9 8 7 6 5 4 3 2 1 You can use (\ \ x --&gt; 0) Output: 9 8 7 6 5 4 3 2 1 0 You can use (\ \ x --&gt; 0 \ \ ) Output: 9 8 7 6 5 4 3 2 1 0 You can use also ( x --&gt; 0 ) Output: 9 8 7 6 5 4 3 2 1 0 Likewise, you can try lot of methods to execute this command successfully.

char sep = '\n' /1\ ; int i = 68 /1 \ ; while (i --- 1\ \ /1/1/1 /1\ /1\ /1\ /1\ /1\ / 1\ / 1 \ / 1 \ / 1 \ /1 /1 \ /1 /1 \ /1 /1 /1/1&gt; 0) std::cout \ &lt;&lt;i&lt;&lt; sep; For larger numbers, C++20 introduces some more advanced looping features. First to catch i we can build an inverse loop-de-loop and deflect it onto the std::ostream. However, the speed of i is implementation-defined, so we can use the new C++20 speed operator &lt;&lt;i&lt;&lt; to speed it up. We must also catch it by building wall, if we don't, i leaves the scope and de referencing it causes undefined behavior. To specify the separator, we can use: std::cout \ sep and there we have a for loop from 67 to 1.

Instead of regular arrow operator (--&gt;) you can use armor-piercing arrow operator: --x&gt; (note those sharp barbs on the arrow tip). It adds +1 to armor piercing, so it finishes the loop 1 iteration faster than regular arrow operator. Try it yourself: int x = 10; while( --x&gt; 0 ) printf("%d ", x);

Here -- is the unary post decrement operator. while (x-- &gt; 0) // x goes to 0 { printf("%d ", x); } In the beginning, the condition will evaluate as (x &gt; 0) // 10 &gt; 0 Now because the condition is true, it will go into the loop with a decremented value x-- // x = 9 That's why the first printed value is 9 And so on. In the last loop x=1, so the condition is true. As per the unary operator, the value changed to x = 0 at the time of print. Now, x = 0, which evaluates the condition (x &gt; 0 ) as false and the while loop exits.

This --&gt; is not an operator at all. We have an operator like -&gt;, but not like --&gt;. It is just a wrong interpretation of while(x-- &gt;0) which simply means x has the post decrement operator and this loop will run till it is greater than zero. Another simple way of writing this code would be while(x--). The while loop will stop whenever it gets a false condition and here there is only one case, i.e., 0. So it will stop when the x value is decremented to zero.

Actually, you can "create" a --&gt; operator just for fun ) class MyClass { class Helper { MyClass* ptr; Helper(MyClass* _this): ptr{_this} {} public: Helper(const Helper&amp;) = delete; Helper(Helper&amp;&amp;) = delete; void operator=(const Helper&amp;) = delete; void operator=(Helper&amp;&amp;) = delete; operator MyClass() { auto tmp = *ptr; tmp._value++; return tmp; } friend MyClass; void operator&gt;(int){std::cout &lt;&lt; "Operator --&gt;" &lt;&lt; std::endl;} }; int _value = 0; public: MyClass() = default; MyClass(int value): _value{value} {} Helper operator--(int) { _value--; return Helper(this); } int get() const noexcept { return _value; } bool operator&gt;(int num) const noexcept { return _value &gt; num; } }; int main() { MyClass obj(5); obj &gt; 1; //operator &gt; obj--; //operator -- MyClass b = obj--; //still works std::cout &lt;&lt; b.get() &lt;&lt; std::endl; //4 std::cout &lt;&lt; obj.get() &lt;&lt; std::endl; //3 b --&gt; 5; //operator --&gt; //But there is at least one problem auto c = obj--; //auto can deduce a private type :( } But as I said it's only for fun ;)

--&gt; is not an operator, it is the juxtaposition of -- (post-decrement) and &gt; (greater than comparison). The loop will look more familiar as: #include &lt;stdio.h&gt; int main() { int x = 10; while (x-- &gt; 0) { // x goes to 0 printf("%d ", x); } } This loop is a classic idiom to enumerate values between 10 (the excluded upper bound) and 0 the included lower bound, useful to iterate over the elements of an array from the last to the first. The initial value 10 is the total number of iterations (for example the length of the array), and one plus the first value used inside the loop. The 0 is the last value of x inside the loop, hence the comment x goes to 0. Note that the value of x after the loop completes is -1. Note also that this loop will operate the same way if x has an unsigned type such as size_t, which is a strong advantage over the naive alternative for (i = length-1; i &gt;= 0; i--). For this reason, I am actually a fan of this surprising syntax: while (x --&gt; 0). I find this idiom eye-catching and elegant, just like for (;;) vs: while (1) (which looks confusingly similar to while (l)). It also works in other languages whose syntax is inspired by C: C++, Objective-C, java, javascript, C# to name a few.

That's what you mean. while((x--) &gt; 0) We heard in childhood, Stop don't, Let Go (روکو مت، جانے دو) Where a Comma makes confusion Stop, don't let go. (روکو، مت جانے دو) Same Happens in Programming now, a SPACE makes confusion. :D