The Secret Operator --> in C++: A Joke That Became a Legend

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What lies behind the mysterious "-->" operator in C++?



Imagine: you go to Stack Overflow and see a question that has gathered thousands of votes and millions of views. The question is simple but intriguing: "What is the '-->' operator in C/C++?". Sounds like something top-secret, right? Maybe it's a new C++23 standard? Or a magical construct that textbooks are silent about?



In reality, this is one of the most famous programmer memes and, at the same time, an excellent opportunity to understand how operators work in C++ and how easy it is to trick your brain when reading code. In this article, we will uncover the mystery of the "--> operator" and show how this example helps beginners understand C++ syntax at a deep level.



What does the mysterious code look like?



Here it is, the very snippet that blew up the internet:



#include <iostream>

int main() { int x = 10; while (x --> 0) { std::cout << x << " "; } return 0;}


Program output:



9 8 7 6 5 4 3 2 1 0


It looks like we are using the --> operator (decrement and arrow). But let's face the truth: there is no "-->" operator in C++. This is an illusion created by how the C++ compiler breaks the code into tokens.



How does the compiler "read" this code?



The C++ compiler follows the maximum munch rule: it always tries to form the longest possible token from the available characters. In the expression x --> 0, the compiler sees not -->, but two separate tokens:



  • Token 1: -- — the decrement operator (decrease by 1)
  • Token 2: > — the "greater than" comparison operator


That is, it's actually not x --> 0, but x-- > 0. The space between -- and > is optional — the compiler will understand that these are two different operators anyway.



Full breakdown: from start to finish



Let's break down this code step by step to understand the program logic:



  1. The variable x is initialized with the value 10.
  2. The loop condition is checked: x-- > 0. This is a postfix decrement: first, the current value of x (10) is taken, compared to 0 (10 > 0 — true), and only then x is decremented to 9.
  3. Since the condition is true, the loop body executes: it prints the already decremented value (9).
  4. The loop repeats until x becomes 0. When x = 0, the condition 0 > 0 is false, and the loop ends.


Note: in the output, we see numbers from 9 down to 0, but we do not see 10. This is because the postfix decrement first uses the old value for comparison, then decrements. This is a common pitfall for beginners, but now you know how it works.



Why is this example so popular?



On Stack Overflow, this question has gathered over 1.5 million views. Why? Because it perfectly demonstrates several important concepts of programming in C++:



  • Lexical analysis — how the compiler breaks code into tokens;
  • Postfix vs prefix decrement — the difference between x-- and --x;
  • Operator precedence — why the code works exactly this way and not otherwise;
  • Code readability — why even "working" code can be misleading.


How to avoid falling for this trick in real code?



The main lesson from this story is write code so that it is easy to read. Never rely on

Blogs

Book Recommendations