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