#include <stdio.h>The output of this program is to simply count down from 9 to 0, printing each value. As many others pointed out, "-->" isn't a single operator at all, but two operators smashed together. The condition of the
int x = 10;
while( x --> 0 ) // x goes to 0
printf("%d ", x);
whileloop above can be rewritten with proper spacing as:
while( x-- > 0 ) // x-- is greater than 0Joshua Bloch commented on the goes-to operator on Twitter yesterday.
He links to the results of a Google Code search for instances of "-->" in the wild. The results are specific to the C programming language, but "-->" turns up in similar searches for C++ and Java as well (and probably any other language that allows it).
What's the harm?
So what's so harmful about this bit of cleverness? The main problem is that it's too clever. Any time you use standard operators in a non-standard way (even if the language specification doesn't strictly forbid it) you're negatively impacting the readability of your code. In this specific case, the "-->" operator isn't documented anywhere. It's just not reasonable to expect other programmers to know what it means.
On top of that, this situation isn't best handled by a
whileloop to begin with. If you know ahead of time how many iterations your loop needs to make, use a
forloop. Kernighan and Richie covered this a very long time ago in The C Programming Language. It's still true today.
TheThis, of course, is also true for a simple decrementing loop. The meaning of the following code should be obvious to any junior programmer.
foris preferable when there is a simple initialization and increment, since it keeps the loop control statements close together and visible at the top of the loop.
for( i = 9; i >= 0; i-- )
printf("%d ", i);
Writing code that's "too clever" for other programmers to understand immediately just obfuscates your code needlessly. You'll seem much cleverer to your colleagues if you write your code in a clear and readable style to begin with. Besides that, using the common idioms of your language is probably the quickest way to reduce your WTFs/minute score at your next code review.