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:
- The post-decrement operator
--applied tox. - The greater-than operator
>comparing the (original) value ofxtoy.
How It Works Step by Step
x--(post-decrement) returns the current value ofxand then decrementsxby 1.>then compares that returned value withy, resulting in eithertrueorfalse.
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:
- Compares the current value of
xto0. - Decrements
ximmediately after using its value in the comparison.
Recommended Courses
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:
Grokking Algorithm Complexity and Big-O
Understand how to analyze time and space complexity, crucial for writing efficient code.Grokking Data Structures & Algorithms for Coding Interviews
Master the fundamental building blocks that top tech companies expect you to know.
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.