What is the '-->' operator in C/C++?
C and C++ obey the "maximal munch" rule. The same way a---b
is translated to (a--) - b
, in your case x-->0
translates to (x--)>0
.
What the rule says essentially is that going left to right, expressions are formed by taking the maximum of characters which will form a valid token.
Instead of regular arrow operator (-->) you can use armor-piercing arrow operator: --x> (note those sharp barbs on the arrow tip). It adds +1 to armor piercing, so it finishes the loop 1 iteration faster than regular arrow operator. Try it yourself:
int x = 10;
while( --x> 0 )
printf("%d ", x);
That's a very complicated operator, so even ISO/IEC JTC1 (Joint Technical Committee 1) placed its description in two different parts of the C++ Standard.
Joking aside, they are two different operators: --
and >
described respectively in §5.2.6/2 and §5.9 of the C++03 Standard.
There is a space missing between --
and >
. x
is post decremented, that is, decremented after checking the condition x>0 ?
.
It's a combination of two operators. First --
is for decrementing the value, and >
is for checking whether the value is greater than the right-hand operand.
#include<stdio.h>
int main()
{
int x = 10;
while (x-- > 0)
printf("%d ",x);
return 0;
}
The output will be:
9 8 7 6 5 4 3 2 1 0
Utterly geek, but I will be using this:
#define as ;while
int main(int argc, char* argv[])
{
int n = atoi(argv[1]);
do printf("n is %d\n", n) as ( n --> 0);
return 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.
The operator you use is called "decrement-and-then-test". It is defined in the C99 standard, which is the latest version of the C programming language standard. The C99 standard added a number of new operators, including the "decrement-and-then-test" operator, to the C language. Many C++ compilers have adopted these new operators as extensions to the C++ language.
Here is how the code without using the "decrement-and-then-test" operator:
#include <stdio.h>
int main()
{
int x = 10;
while (x > 0)
{
printf("%d ", x);
x--;
}
}
In this version of the code, the while
loop uses the >
operator to test whether x
is greater than 0. The x--
statement is used to decrement x
by 1 at the end of each iteration of the loop.
Or for something completely different... x
slides to 0
.
while (x --\
\
\
\
> 0)
printf("%d ", x);
Not so mathematical, but... every picture paints a thousand words...
while( x-- > 0 )
is how that's parsed.
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.
Conventional way we define condition in while loop parenthesis"()
" and terminating condition inside the braces"{}
", but this --
& >
is a way one defines all at once.
For example:
int abc(){
int a = 5
while((a--) > 0){ // Decrement and comparison both at once
// Code
}
}
It says, decrement a
and run the loop till the time a
is greater than 0
Other way it should have been like:
int abc() {
int a = 5;
while(a > 0) {
a = a -1 // Decrement inside loop
// Code
}
}
Both ways, we do the same thing and achieve the same goals.
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 makes the things look funny, --
decrements and >
compares.
This -->
is not an operator at all. We have an operator like ->
, but not like -->
. It is just a wrong interpretation of while(x-- >0)
which simply means x has the post decrement operator and this loop will run till it is greater than zero.
Another simple way of writing this code would be while(x--)
. The while loop will stop whenever it gets a false condition and here there is only one case, i.e., 0
. So it will stop when the x value is decremented to zero.
Here --
is the unary post decrement operator.
while (x-- > 0) // x goes to 0
{
printf("%d ", x);
}
(x > 0) // 10 > 0
x-- // x = 9
x=1
, so the condition is true. As per the unary operator, the value changed to x = 0
at the time of print.x = 0
, which evaluates the condition (x > 0 )
as false and the while loop exits.It's equivalent to
while (x-- > 0)
x--
(post decrement) is equivalent to x = x-1
(but returning the original value of x
), so the code transforms to:
while(x > 0) {
x = x-1;
// logic
}
x--; // The post decrement done when x <= 0
-->
is not an operator. It is in fact two separate operators, --
and >
.
The conditional's code 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 )
That's what you mean.
while((x--) > 0)
We heard in childhood,
Stop don't, Let Go (روکو مت، جانے دو)
Where a Comma makes confusion
Stop, don't let go. (روکو، مت جانے دو)
Same Happens in Programming now, a SPACE makes confusion. :D
--
is the decrement operator and >
is the greater-than operator.
The two operators are applied as a single one like -->
.
Why all the complication?
The simple answer to the original question is just:
#include <stdio.h>
int main()
{
int x = 10;
while (x > 0)
{
printf("%d ", x);
x = x-1;
}
}
It does the same thing. I am not saying you should do it like this, but it does the same thing and would have answered the question in one post.
The x--
is just shorthand for the above, and >
is just a normal greater-than operator
. No big mystery!
There are too many people making simple things complicated nowadays ;)
After reading Hidden Features and Dark Corners of C++/STL on 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 <stdio.h>
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 0
Where is this defined in the standard, and where has it come from?