10074

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. I would assume this is also valid C since it works in GCC as well.

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

Where is this defined in the standard, and where has it come from?

user16217248
  • 3,119
  • 19
  • 19
  • 37
GManNickG
  • 494,350
  • 52
  • 494
  • 543

28 Answers28

9761

--> is not an operator. It is in fact two separate operators, -- and >.

The code in the condition 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 )
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 74
    I've seen it jokingly referred to as the "downto" operator (https://codegolf.stackexchange.com/questions/16226/i-need-a-program-where-the-user-inputs-an-array-of-doubles-and-the-program-outpu/16232#16232) – Moshe Bildner May 19 '21 at 13:26
  • 14
    I think you wouldn't really *need* the parentheses around `x--` though it does further enforce the separation. It would probably be enough just to associate tokens more clearly with something like `while (x-- > 0)`. – paxdiablo Jul 03 '21 at 01:18
  • 13
    I think, Stack Overflow needs a separate category like "C++ syntax jokes" or something like that. – CuriousPan Jun 10 '22 at 14:13
  • 4
    The most important reason to use this operator is I believe to avoid the "off-by-one" errors that occur when traversing an array in reverse It's quite easier to use for(int i = n; i-->0;) then for(int i = n-1; i >= 0; i--). Additionally, for unsigned the second for won't even work, and the first for is one of the easiest ways I've seen it written for unsigned.For example for(size_t i = n; i-->0;) – Vineet Nov 08 '22 at 21:16
3902

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

Dr. Gut
  • 2,053
  • 7
  • 26
unsynchronized
  • 4,828
  • 2
  • 31
  • 43
2592

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.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
  • I think you'd find usage of the '-->' operator somewhere described in ancient penal law... – nick Oct 13 '22 at 08:24
1777

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

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

;)

mip
  • 8,355
  • 6
  • 53
  • 72
1455

It's equivalent to

while (x-- > 0)

x-- (post decrement) is equivalent to x = x-1 (but returning the original value of x), so the code transforms to:

while(x > 0) {
    x = x-1;
    // logic
}
x--;   // The post decrement done when x <= 0
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
612

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.

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
RageZ
  • 26,800
  • 12
  • 67
  • 76
496

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.

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 565
    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. – burito Dec 30 '09 at 05:16
  • 41
    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). – Joey Adams Apr 12 '10 at 15:07
  • 1
    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. – Bernhard Barker May 22 '15 at 12:55
  • 12
    In x86 ASM, the `LOOP
    ` decreases the `ECX` register, then jumps to `
    ` 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.
    – Mark K Cowan Jul 08 '15 at 11:26
  • 6
    Continuing my previous comment: `MOV ECX, value`, `@start:`, ``, `LOOP @start` is an x86 ASM equivalent for `counter = value - 1; while (counter --> 0) { ; }`. Note that it will barf if `value` is initially zero, so an extra check is needed pre-loop. – Mark K Cowan Jul 08 '15 at 11:32
  • 1
    LOOP was never particularly fast, though. – Remember Monica Apr 01 '22 at 18:48
434

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;
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Escualo
  • 40,844
  • 23
  • 87
  • 135
  • 18
    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." – StevePoling Mar 07 '21 at 19:43
  • 4
    Calling `atoi` makes the code smell immediately. – Roland Illig Jan 24 '22 at 17:24
  • 9
    @StevePoling Intent is being communicated. When I read this code, I understand that I am being trolled. – FriskySaga Dec 08 '22 at 20:06
426
while( x-- > 0 )

is how that's parsed.

TKirishima
  • 742
  • 6
  • 18
Grumdrig
  • 16,588
  • 14
  • 58
  • 69
369

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
Halo
  • 1,730
  • 1
  • 8
  • 31
NguyenDat
  • 4,129
  • 3
  • 46
  • 46
299

This is exactly the same as

while (x--)
Ravi
  • 372
  • 1
  • 7
  • 24
Good Person
  • 1,437
  • 2
  • 23
  • 42
  • 12
    @DoctorT that's what `unsigned` is for – Cole Tobin Mar 23 '13 at 18:39
  • 11
    `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`. – chqrlie Mar 12 '21 at 17:14
  • @Adrien: I beg to differ: if `x` is negative, UB only happens when `x == INT_MIN`, at least `x - INT_MIN` iterations occur before this... Conversely, if `x` has an unsigned type, no undefined behavior happens for arithmetic operations: they are computed modulo 2 to the power . – chqrlie Sep 11 '21 at 17:27
  • @chqrlie Undefined behaviour can have retroactive effects. The question is not _when_ UB occurs, but _whether_. – wizzwizz4 Apr 02 '23 at 22:21
264

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.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Test
  • 1,697
  • 1
  • 11
  • 10
  • 24
    Goes to cant be true always especially when value of x is negative. – Ganesh Gopalasubramanian Nov 13 '09 at 03:22
  • 18
    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. – Pete Kirkham Jun 21 '10 at 08:57
  • 9
    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. – tslmy Jun 15 '13 at 02:49
  • 25
    @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. – SamB Dec 06 '13 at 06:57
  • 4
    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. – Marc van Leeuwen Aug 30 '14 at 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)`. – Marc van Leeuwen Aug 30 '14 at 20:16
  • 1
    `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--)` . – Horse SMith Nov 21 '14 at 02:12
  • 1
    Well, also `while(0 <=-- x)`. But this's getting silly(er) :p – Noein May 26 '15 at 07:57
  • 1
    With any level of compiler optimisation, there is **no difference in efficiency.** The assembly output is identical. – Artelius Aug 21 '20 at 01:36
  • Is `x--` defined behavior if x is unsigned and 0? Naively, I'd expect `x` to become -1, which doesn't fit in an unsigned and thus be undefined. – Troy Daniels Dec 15 '21 at 19:42
  • @TroyDaniels unsigned overflow is, in fact, defined behavior. – SamB Aug 14 '23 at 02:50
242

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
199

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.

John Odom
  • 1,189
  • 2
  • 20
  • 35
Stone Mason
  • 2,015
  • 2
  • 16
  • 19
  • 6
    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. – Tim Leaf May 05 '10 at 15:30
  • 18
    `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. – Bernhard Barker May 22 '15 at 12:28
  • 1
    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`. – Mark Lakata Feb 24 '20 at 16:41
167

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 49
    The space is not missing - C(++) ignores whitespace. –  Aug 02 '12 at 19:16
  • 31
    @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 ()`. – Jens Apr 25 '13 at 21:16
  • 33
    @Jens How about: "The space is not missing - C(++) ignores unnecessary white space."? – Kevin P. Rice Dec 04 '13 at 20:35
  • 2
    `#define` is processed by the preprocessor – the compiler does not actually see this – and thus the whitespace in the C code itself (without preprocessor lines) is irrelevant. – xdevs23 Jun 11 '22 at 21:34
155

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

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

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
sam
  • 386
  • 1
  • 3
  • 10
  • 18
    They're _applied_ as the 2 separate operators they are. They're only _written_ misleadingly to look like "a single one". – underscore_d Nov 12 '16 at 17:56
147

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            
John Odom
  • 1,189
  • 2
  • 20
  • 35
Rajeev Das
  • 1,581
  • 4
  • 18
  • 21
145

C and C++ obey the "maximal 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 a valid token.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pandrei
  • 4,843
  • 3
  • 27
  • 44
135

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Salman
  • 1,380
  • 7
  • 25
  • 41
  • 8
    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. – Florian F Sep 01 '14 at 09:46
  • 2
    @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: https://blogs.msdn.microsoft.com/ericlippert/2010/04/01/some-last-minute-new-c-4-0-features/ – underscore_d Nov 12 '16 at 17:57
67

Instead of regular arrow operator (-->) you can use armor-piercing arrow operator: --x> (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> 0 )
    printf("%d ", x);
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
hydrechan
  • 71
  • 2
  • 7
35

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 ;)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Garry_G
  • 169
  • 5
  • 12
  • 22
    This question is not about complications, but about ** Hidden Features and Dark Corners of C++/STL** – pix Oct 27 '16 at 15:32
  • 27
    The program here gives different output than original because x here is decremented after printf. That demonstrates well how "simple answers" are usually Incorrect. – Öö Tiib May 13 '17 at 09:30
  • 3
    `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` – Anthony Dec 15 '17 at 18:33
  • 3
    It doesn't do the same thing. Move your `x=x-1` before `printf` then you can say "it does the same thing". – CITBL Jan 05 '19 at 17:05
  • 1
    I agree with your attitude about simple and complicated, however `x=x-1` is definitely more complicated than `x--` (more characters to type, harder to read because people wonder why you're not just using good old `x--`, etc.) Also, C programmers who consider things like side effects in `while` to be overly complicated tend to make me suspicious. – Wolfram Rösler Nov 17 '21 at 11:06
32

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.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Zohaib Ejaz
  • 387
  • 4
  • 18
  • 5
    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'. – v010dya Jul 14 '17 at 19:07
  • @S.S.Anne Your edit is still wrong. The `a--` after the while shouldn't be there. – Stefan Fabian Dec 29 '20 at 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. – chqrlie Mar 12 '21 at 12:20
30

(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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kalana
  • 5,631
  • 7
  • 30
  • 51
6

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

chqrlie
  • 131,814
  • 10
  • 121
  • 189
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ankit Mishra
  • 530
  • 8
  • 16
5

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Neeraj Bansal
  • 380
  • 7
  • 23
-1

That's what you mean.

while((x--) > 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

Numan Gillani
  • 489
  • 9
  • 19
  • This idea can be abused for distant targets in a bow and arrow style: `while((x --)> 0)` – chqrlie Jun 02 '21 at 08:16
  • That's upto the understanding, whatever seems easy & understandable to a person, it's fine for him/her. Main Goal is to clear the concept and be a successful developer :) – Numan Gillani Jun 02 '21 at 08:21
  • Of course. And IMHO `while (x --> 0)` is clear and effective. `x` takes all values below the starting one down to and including `0`, which is perfect for a loop enumerating index values for an array for both signed and unsigned types of `x`. – chqrlie Jun 02 '21 at 08:28
-4

The operator you use is called "decrement-and-then-test". It is defined in the C99 standard, which is the latest version of the C programming language standard. The C99 standard added a number of new operators, including the "decrement-and-then-test" operator, to the C language. Many C++ compilers have adopted these new operators as extensions to the C++ language.

Here is how the code without using the "decrement-and-then-test" operator:

#include <stdio.h>
int main()
{
    int x = 10;
    while (x > 0)
    {
        printf("%d ", x);
        x--;
    }
}

In this version of the code, the while loop uses the > operator to test whether x is greater than 0. The x-- statement is used to decrement x by 1 at the end of each iteration of the loop.

  • 1
    I'm afraid your code is not equivalent to the `while (x-->0)` loop: the `x--;` statement must be moved **before** the `printf` statement and also inserted **after** the `while` loop body. – chqrlie Dec 08 '22 at 10:22