patterncppCritical
What is the '-->' operator in C/C++?
Viewed 0 times
operatorwhatthe
Problem
After reading Hidden Features and Dark Corners of C++/STL on
Here's the code:
Output:
Where is this defined in the standard, and where has it come from?
comp.lang.c++.moderated, I was completely surprised that the following snippet compiled and worked in both Visual Studio 2008 and G++ 4.4. I would assume this is also valid C since it works in GCC as well.Here's the code:
#include
int main()
{
int x = 10;
while (x --> 0) // x goes to 0
{
printf("%d ", x);
}
}Output:
9 8 7 6 5 4 3 2 1 0Where is this defined in the standard, and where has it come from?
Solution
--> is not an operator. It is in fact two separate operators, -- and >.The code in the condition decrements
x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.To better understand, the statement could be written as follows:
while( (x--) > 0 )
Context
Stack Overflow Q#1642028, score: 9961
Revisions (0)
No revisions yet.