29
After reading "Hidden Features and Dark Corners of C++/STL" on comp.lang.c++.moderated, I was completely surprised that it compiled and worked in both Visual Studio 2008 and G++ 4.4. The code:
I'd assume this is C, since it works in GCC as well. Where in the standard is this defined, and where did it come from?
improve this question
|
comment
0
✓
Its combination of two operators.First "--" is for decrementing the value and ">" for checking greater than value.
#include<stdio.h>
int main()
{
int x = 10;
while (x-- > 0)
printf("%d ",x);
return 0;
}
output will be:
9 8 7 6 5 4 3 2 1 0
0
Anyway, we have a "goes to" operator now. "-->" is easy to be remembered as a direction, and "while x goes to zero" is meaning-straight.
Furthermore, it is a little more efficient than "for (x = 10; x > 0; x --)" on some platforms.
0
That's not an operator -->. That's two separate operators, -- and >.
Your condition code is decrementing x, while returning xs original (not decremented) value, and then comparing the original value with 0 using the > operator.
To better understand, the statement could be as follows:
while( (x--) > 0 )
0
That's a very complicated operator, so C++ Standard committee even placed its description in two different parts of Standard.
Joking aside, they are two different operators: -- and > described respectively in 5.2.6/2 and 5.9 of the C++03 Standard.
0
It's equivalent to
while (x-- > 0)
0
It's
#include <stdio.h>
int main(void)
{
int x = 10;
while( x-- > 0 ) // x goes to 0
{
printf("%d ", x);
}
return 0;
}
Just the space make the things look funny, -- decrements and > compare.
0
while( x-- > 0 )
is how that's parsed.
0
In one book I read they said (I don't remember correctly what is book): Compiler try to parse expression to bigest token by using left right rule:
In this case is:
x-->0
Parse to biggest token:
token 1: x
token 2: --
token 3: >
token 4: 0
conclude: x-- > 0
Same as:
a-----b
After parse
token 1: a
token 2: --
token 3: --
token 4: -
token 5: b
conclude: (a--)-- - b
I hope it helps to understand the complicated expression ^^
0
This is exactly the same as
while (x--){
printf("%d ", x);
}
for non-negative numbers
0
This code first compare x and 0 then decrement x . [ also say in first answer : You're post decrementing x and then comparing x and 0 with the > operator ]
see the output of this code :
9 8 7 6 5 4 3 2 1 0
we know first compare and then decrement by see 0 in output .
if we want to first decrement and then compare use this code :
#include <stdio.h>
int main(void)
{
int x = 10;
while( --x> 0 ) // x goes to 0
{
printf("%d ", x);
}
return 0;
}
that output is :
9 8 7 6 5 4 3 2 1
0
There is a space missing between -- and >. x is post decremented, that is, decremented after checking the condition x>0 ?.
0
My compiler will print out 9876543210 when I run this code.
#include <iostream>
int main()
{
int x = 10;
while( x --> 0 ) // x goes to 0
{
std::cout << x;
}
}
As expected. The while( x-- > 0 ) actually means while( x > 0). The x-- post decrements x.
while( x > 0 ) {
x--;
std::cout << x;
}
is a different way of writing the same thing.
It is nice that the original looks like "while x goes to 0" though.
0
-- is the decrement operator and > is the greater-than operator.
The two operators are applied as a single one like -->.
0
Actually x is post-decrementing and with that condition is being checked. its not -->, its (x--) > 0
Note: value of x is changed after condition is checked because it post-decrementing. Some similar cases can also occur e.g.
--> x-->0
++> x++>0
-->= x-->=0
++>= x++>=0
0
It is to prevent confusion (obfuscation, perceived cleverness etc) such as this, that static analysis checkers (eg) MISRA seeks to prevent assignment operations (including ++ and -- as well as the obvious =) in conditional statements.
0
The usage of --> has historical relevance. Decrementing was (and still is in some cases), faster than incrementing on the x86 architecture. Using --> suggests that x is going to 0, and appeals to those with mathematical backgrounds.