Quick answer: there is no --> operator in C or C++. What you're really
seeing is two separate, ordinary operators sitting next to each other: the postfix decrement
-- and the greater-than comparison >. The compiler parses
x --> 0 as x-- > 0.
The classic example
#include <stdio.h>
int main(void) {
int x = 10;
while (x --> 0) { // read as: x-- > 0
printf("%d ", x);
}
}
// prints: 9 8 7 6 5 4 3 2 1 0
Why the confusion?
C's tokenizer is greedy and whitespace-insensitive: x-- > 0, x --> 0, and
x-->0 all tokenize identically. Visually it looks like an arrow pointing "toward zero," which
is a fun mnemonic but not a real language feature — it's just -- followed by
>.
Related lookalikes
->is a real operator: member access through a pointer, e.g.ptr->field.<--is not special either; it's<followed by--.
Why it starts at 9, not 10
Postfix x-- evaluates to the value of x before decrementing, so the
first comparison checks 10 > 0 (true), then afterward decrements x to
9, which is what gets printed. That's why the output starts at 9 rather than 10.
FAQ
Is --> a real C++ operator?
No. It's the decrement operator -- immediately followed by the greater-than operator
>, tokenized independently by the compiler.
Does this work in other languages?
Any C-family language with a postfix decrement and a greater-than operator parses it the same way (Java, C#, JavaScript, etc.), since it's a tokenizing quirk, not a special feature.
This article explains and expands on the community answers to the Stack Overflow question “What is the '-->' operator in C/C++?”, used under the CC BY-SA 4.0 license. Screenshot credit: Stack Exchange Inc.
No comments
Post a Comment