What is the "-->" operator in C/C++?

Created 29.10.2009 06:57
Viewed 854K times
9385 votes

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?

0
Answers 26
0
9076

--> 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 )
29.10.2009 07:00
0
3420

Or for something completely different... x slides to 0.

while (x --\
            \
             \
              \
               > 0)
     printf("%d ", x);

Not so mathematical, but... every picture paints a thousand words...

18.01.2012 11:18
0
2451

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 > described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.

29.10.2009 08:38
2
1383

x can go to zero even faster in the opposite direction:

int x = 10;

while( 0 <---- x )
{
   printf("%d ", x);
}

8 6 4 2

You can control speed with an arrow!

int x = 100;

while( 0 <-------------------- x )
{
   printf("%d ", x);
}

90 80 70 60 50 40 30 20 10

;)

28.12.2014 00:32
Comments
"You can control speed with an arrow!". Thanks, I hate it. by DeusXMachina, 15.02.2021 23:52
An apt answer to the question. by paradocslover, 19.03.2021 04:30
0
1339

It's equivalent to

while (x-- > 0)

x-- (post decrement) is equivalent to x = x-1 so, the code transforms to:

while(x > 0) {
    x = x-1;
    // logic
}
x--;   // The post decrement done when x <= 0
29.10.2009 07:00
0
570

It's

#include <stdio.h>

int main(void) {
  int x = 10;
  while (x-- > 0) { // x goes to 0
    printf("%d ", x);
  }
  return 0;
}

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

29.10.2009 07:00
5
451

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

18.11.2009 12:47
Comments
Not exactly true. Decrementing and Incrementing take the same amount of time, the benefit of this is that comparison to zero is very fast compared to comparison versus a variable. This is true for many architectures, not just x86. Anything with a JZ instruction (jump if zero). Poking around you can find many "for" loops that are written backwards to save cycles on the compare. This is particularly fast on x86 as the act of decrementing the variable set the zero flag appropriately, so you could then branch without having to explicitly compare the variable. by burito, 30.12.2009 05:16
Well, decrementing toward zero means you only have to compare against 0 per loop iteration, while iterating toward n means comparing with n each iteration. The former tends to be easier (and on some architectures, is automatically tested after every data register operation). by Joey Adams, 12.04.2010 15:07
This would be better as a footnote in another answer or a comment - it clearly doesn't explain what --> means, which is what was asked. by Bernhard Barker, 22.05.2015 12:55
In x86 ASM, the LOOP <address> decreases the ECX register, then jumps to <address> unless the decrementing of ECX resulted in zero. Decrementing the loop counter towards zero allows the compiler to generate a single LOOP instruction, whereas incrementing or counting to other values requires separate INC/DEC/ADD/SUB, compare, and conditional jump instructions. Modern compilers can often convert other loops to a counter --> 0 loop if the value of counter isn't used in the loop. by Mark K Cowan, 08.07.2015 11:26
Continuing my previous comment: MOV ECX, value, @start:, <code>, LOOP @start is an x86 ASM equivalent for counter = value - 1; while (counter --> 0) { <code>; }. Note that it will barf if value is initially zero, so an extra check is needed pre-loop. by Mark K Cowan, 08.07.2015 11:32
1
389

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 --> 0);
    return 0;
}
18.05.2010 20:33
Comments
I know it looks way cool, but I fear it's deceptive. The reason you're writing C++ instead of Machine Language is b/c you want to convey your intent to the next guy reading your code. This construct violates the principle of least surprise. It is a mental "trip hazard." by StevePoling, 07.03.2021 19:43
0
384
while( x-- > 0 )

is how that's parsed.

29.10.2009 07:00
0
334

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

Parses to biggest tokens:

token 1: x
token 2: --
token 3: >
token 4: 0
conclude: x-- > 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 ^^

09.04.2010 00:04
3
284

This is exactly the same as

while (x--)
31.12.2009 13:48
Comments
Shouldn't this be for(--x++;--x;++x--)? by Mateen Ulhaq, 04.12.2011 21:32
@DoctorT that's what unsigned is for by Cole Johnson, 23.03.2013 18:39
while (x --> 0) is not the same as while (x--) if x has a signed type, the first loop does not execute at all if x has a negative value but the second iterates many times until it hits undefined behavior when x reaches INT_MIN. by chqrlie, 12.03.2021 17:14
9
250

Anyway, we have a "goes to" operator now. "-->" 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 > 0; x --)" on some platforms.

29.10.2009 14:45
Comments
Goes to cant be true always especially when value of x is negative. by Ganesh Gopalasubramanian, 13.11.2009 03:22
The other version does not do the same thing - with for (size_t x=10; x-->0; ) the body of the loop is executed with 9,8,..,0 whereas the other version has 10,9,..,1. It's quite tricky to exit a loop down to zero with an unsigned variable otherwise. by Pete Kirkham, 21.06.2010 08:57
I think this is a little bit misleading... We don't have a literally "goes to" operator, since we need another ++> to do the incremental work. by tslmy, 15.06.2013 02:49
@Josh: actually, overflow gives undefined behavior for int, so it could just as easily eat your dog as take x to zero if it starts out negative. by SamB, 06.12.2013 06:57
This is a very important idiom to me for the reason given in the comnmet by @PeteKirkham, as I often need to do decreasing loops over unsigned quantities all the way to 0. (For comparison, the idiom of omitting tests for zero, such as writing while (n--) instead for unsigned n, buys you nothing and for me greatly hampers readability.) It also has the pleasant property that you specify one more than the initial index, which is usually what you want (e.g., for a loop over an array you specify its size). I also like --> without space, as this makes the idiom easy to recognise. by Marc van Leeuwen, 30.08.2014 20:08
... Actually, I somewhat regret that writing while(0<--n) does not do the same thing as writing while(n-->0), and the former is usually not what you want. On the other hand, at least in C++, you can make it descend twice as fast with while(0<----n). by Marc van Leeuwen, 30.08.2014 20:16
x=10; while(x --> 0) is not the same as for(x=10; x>0; x--)... it would on the other hand be the same as for(x=10-1; x=>0; x--) . by Horse SMith, 21.11.2014 02:12
Well, also while(0 <=-- x). But this's getting silly(er) :p by Narfanar, 26.05.2015 07:57
With any level of compiler optimisation, there is no difference in efficiency. The assembly output is identical. by Artelius, 21.08.2020 01:36
Show remaining 4 comments
1
227

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 > 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 <stdio.h>
int main(void)
{
    int x = 10;

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

That output is:

9 8 7 6 5 4 3 2 1
18.11.2009 12:52
Comments
--x> 0 Is that x in a spaceship going towards 0 (the moon)? by H-005, 31.07.2020 06:02
3
186

My compiler will print out 9876543210 when I run this code.

#include <iostream>
int main()
{
    int x = 10;

    while( x --> 0 ) // x goes to 0
    {
        std::cout << x;
    }
}

As expected. The while( x-- > 0 ) actually means while( x > 0). The x-- post decrements x.

while( x > 0 ) 
{
    x--;
    std::cout << x;
}

is a different way of writing the same thing.

It is nice that the original looks like "while x goes to 0" though.

17.01.2010 00:46
Comments
The result is only undefined when you're incrementing/decrementing the same variable more than once in the same statement. It doesn't apply to this situation. by Tim Leaf, 05.05.2010 15:30
while( x-- > 0 ) actually means while( x > 0) - I'm not sure what you were trying to say there, but the way you phrased it implies the -- has no meaning whatsoever, which is obviously very wrong. by Bernhard Barker, 22.05.2015 12:28
To drive the point home from @Dukeling, this answer is not the same as the original post. In the original post, x will be -1 after it leaves the loop, while in this answer, x will be 0. by Mark Lakata, 24.02.2020 16:41
3
155

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

28.12.2009 16:32
Comments
The space is not missing - C(++) ignores whitespace. by User, 02.08.2012 19:16
@H2CO3 This isn't true in general. There are places where white space must be used to separate tokens, e.g. in #define foo() versus #define foo (). by Jens, 25.04.2013 21:16
@Jens How about: "The space is not missing - C(++) ignores unnecessary white space."? by Kevin P. Rice, 04.12.2013 20:35
1
144

-- is the decrement operator and > is the greater-than operator.

The two operators are applied as a single one like -->.

06.04.2010 10:45
Comments
They're applied as the 2 separate operators they are. They're only written misleadingly to look like "a single one". by underscore_d, 12.11.2016 17:56
0
134

It's a combination of two operators. First -- is for decrementing the value, and > is for checking whether the value is greater than the right-hand operand.

#include<stdio.h>

int main()
{
    int x = 10;

    while (x-- > 0)
        printf("%d ",x);

    return 0;
}

The output will be:

9 8 7 6 5 4 3 2 1 0            
02.04.2013 11:22
2
125

Actually, x is post-decrementing and with that condition is being checked. It's not -->, it's (x--) > 0

Note: value of x is changed after the condition is checked, because it post-decrementing. Some similar cases can also occur, for example:

-->    x-->0
++>    x++>0
-->=   x-->=0
++>=   x++>=0
18.08.2012 07:34
Comments
Except that ++> can hardly be used in a while(). A "goes up to..." operator would be ++<, which doesn't look anywhere as nice. The operator --> is a happy coincidence. by Florian F, 01.09.2014 09:46
@BenLeggiero That could 'work' in the sense of generating code that does something (while infuriating readers who don't like faux-clever code), but the semantics are different, as its use of predecrement means it will execute one fewer iteration. As a contrived example, it would never execute the loop body if x started at 1, but while ( (x--) > 0 ) would. {edit} Eric Lippert covered both in his C# 4 release notes: blogs.msdn.microsoft.com/ericlippert/2010/04/01/… by underscore_d, 12.11.2016 17:57
3
123

C and C++ obey the "maximum munch" rule. The same way a---b is translated to (a--) - b, in your case x-->0 translates to (x--)>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.

10.02.2014 17:29
Comments
Which is what the OP assumed: that "((a)-->)" was the maximal munch. It turns out that the OP's original assumption was incorrect: "-->" is not a maximum valid operator. by david, 28.08.2014 00:41
Also known as greedy parsing, if I recall correctly. by Roy Tinker, 11.07.2015 01:04
@RoyTinker Greedy scanning. The parser has nothing to do with this. by user207421, 11.09.2016 02:21
4
30

Why all the complication?

The simple answer to the original question is just:

#include <stdio.h>

int main()
{
    int x = 10;
    while (x > 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 > is just a normal greater-than operator. No big mystery!

There are too many people making simple things complicated nowadays ;)

27.10.2016 15:09
Comments
This question is not about complications, but about ** Hidden Features and Dark Corners of C++/STL** by pix, 27.10.2016 15:32
The program here gives different output than original because x here is decremented after printf. That demonstrates well how "simple answers" are usually Incorrect. by Öö Tiib, 13.05.2017 09:30
The OP's way: 9 8 7 6 5 4 3 2 1 0 and The Garry_G way: 10 9 8 7 6 5 4 3 2 1 by Anthony, 15.12.2017 18:33
It doesn't do the same thing. Move your x=x-1 before printf then you can say "it does the same thing". by CITBL, 05.01.2019 17:05
4
27

Conventional way we define condition in while loop parenthesis"()" and terminating condition inside the braces"{}", but this -- & > is a way one defines all at once. For example:

int abc(){
    int a = 5
    while((a--) > 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 > 0) {
        a = a -1 // Decrement inside loop
        // Code
    }
}

Both ways, we do the same thing and achieve the same goals.

28.05.2017 20:10
Comments
This is incorrect. The code in the question does: 'test-write-execute' (test first, write new value, execute the loop), your example is 'test-execute-write'. by v010dya, 14.07.2017 19:07
@v010dya Fixed the answer, now it's test-write-execute as in the question, thanks for pointing out! by Kotauskas, 12.05.2019 10:59
@S.S.Anne Your edit is still wrong. The a-- after the while shouldn't be there. by Stefan Fabian, 29.12.2020 09:49
Both ways, we do the same thing and achieve the same goals. Not really: both loops iterate 5 times, but the final value of a after the loop completes is -1 in the first case and 0 in the second. by chqrlie, 12.03.2021 12:20
0
20

(x --> 0) means (x-- > 0).

  1. You can use (x -->)
    Output: 9 8 7 6 5 4 3 2 1 0
  1. You can use (-- x > 0) It's mean (--x > 0)
    Output: 9 8 7 6 5 4 3 2 1
  1. You can use
(--\
    \
     x > 0)

Output: 9 8 7 6 5 4 3 2 1

  1. You can use
(\
  \
   x --> 0)

Output: 9 8 7 6 5 4 3 2 1 0

  1. You can use
(\
  \
   x --> 0
          \
           \
            )

Output: 9 8 7 6 5 4 3 2 1 0

  1. You can use also
(
 x 
  --> 
      0
       )

Output: 9 8 7 6 5 4 3 2 1 0

Likewise, you can try lot of methods to execute this command successfully.

23.09.2019 12:39
0
10
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> 0) std::cout \
                              <<i<<                               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 <<i<< 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.

23.01.2021 07:32
0
2

Here -- is the unary post decrement operator.

 while (x-- > 0) // x goes to 0
 {
     printf("%d ", x);
 }
  • In the beginning, the condition will evaluate as (x > 0) // 10 > 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 > 0 ) as false and the while loop exits.
06.07.2020 11:28
0
2

This --> is not an operator at all. We have an operator like ->, but not like -->. It is just a wrong interpretation of while(x-- >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.

15.06.2020 18:15
5
1

--> is not an operator, it is the juxtaposition of -- (post-decrement) and > (greater than comparison).

The loop will look more familiar as:

#include <stdio.h>
int main() {
    int x = 10;
    while (x-- > 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 >= 0; i--).

For this reason, I am actually a fan of this surprising syntax: while (x --> 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.

12.03.2021 12:44
Comments
I wonder why this answer was automatically made a community wiki... by chqrlie, 12.03.2021 17:20
You have enough reputation to know: a) this is a community wiki question, so all answers are community wiki, and b) this answer is just a duplication of the plethora of existing answers, on a decade old question. by GManNickG, 12.03.2021 20:42
@GManNickG: I was unaware of the implicit connection between questions and answers for the community wiki status, not everything is obvious, even after thousands of hours contributing to the site, but I am not too old to learn. Regarding the answer paraphrasing other answers, I just wanted to underscore an aspect not addressed by other answers and non obvious for casual readers: while (n-- > 0) is perfect for unsigned types. by chqrlie, 12.03.2021 21:11
Fair enough. Perhaps a comment on an existing answer is more appropriate? by GManNickG, 12.03.2021 21:34
@GManNickG: I did comment about signed / unsigned differences on GoodPerson's answer. My answer illustrates how this loop is simple and appropriate to enumerate array members. None of the answers address this either... let's see if other moderators see fit to delete it. by chqrlie, 12.03.2021 21:37