Logo

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

In C and C++, there is no single operator spelled "-->." When you see something like x-->y, the compiler interprets it as:

x-- > y

This expression consists of:

  1. The post-decrement operator -- applied to x.
  2. The greater-than operator > comparing the (original) value of x to y.

How It Works Step by Step

  • x-- (post-decrement) returns the current value of x and then decrements x by 1.
  • > then compares that returned value with y, resulting in either true or false.

In other words, if you write:

if (x-->y) { // do something }

this is exactly the same as:

if ((x--) > y) { // do something }

Common Example

You may sometimes see code like:

while (x-->0) { // Loop until x is no longer greater than 0 }

This code snippet:

  1. Compares the current value of x to 0.
  2. Decrements x immediately after using its value in the comparison.

Not an Actual Operator

It’s important to remember that "-->" isn’t a standalone operator the way "->" (arrow for pointers) is. It’s merely a tricky placement of the post-decrement and greater-than operators side by side.

Sharpen Your C/C++ and Algorithm Knowledge

If you want to dive deeper into language nuances and performance considerations, check out these resources:

Need a real-world interview practice session? Consider a Coding Mock Interview at DesignGurus.io, where ex-FAANG engineers provide direct feedback on your problem-solving approach and coding style.

Final Thoughts

Seeing "-->" in C/C++ can be confusing if you’re not aware of how the language parses consecutive symbols. Remember: there is no “-->” operator. It’s just the post-decrement -- followed by a greater-than >, a quirk that sometimes appears in loop conditions or comparisons.

CONTRIBUTOR
TechGrind