Guy Rutenberg

Keeping track of what I do

C’s “Goes To” Operator

with 5 comments

Well it isn’t really an operator but this is a nice C code construct I ran into. It doesn’t seem to have any benefit except as a nice way to create a reverse loop:

int count = 100;
while (count-->0) {
        //some work
}

As I said I don’t think I’ll find any performance benefit for using this code snippet. On the other hand it is always fun to see the puzzled face other programmers have the first time they see the code.

Share and Enjoy:
  • del.icio.us
  • StumbleUpon
  • Digg
  • Facebook
  • Mixx
  • Google Bookmarks
  • Simpy

Written by Guy

November 19th, 2007 at 9:24 pm

Posted in C/C++

5 Responses to 'C’s “Goes To” Operator'

Subscribe to comments with RSS or TrackBack to 'C’s “Goes To” Operator'.

  1. error: invalid operands of types ‘’ and ‘int’ to binary ‘operator 0 )
    count

    Miroslav Mollov

    4 Mar 08 at 15:51

  2. Hi Miroslav,
    I had an excess space there, it’s now fixed and should compile correctly.

    Guy

    4 Mar 08 at 16:10

  3. I don’t get it – why do you think this is an operator?…it is just the decrementing the count variable and testing whether it is greater than 0, so:

    count–; //decrement
    (count > 0) //and test

    (count– > 0) (on one line)

    thornza

    25 Mar 08 at 13:49

  4. Of course it’s not a real C operator. This just a nice syntax that resembles the limit sign used in mathematics. When you got x–>0 you call it x goes to 0 so this looked to me similar to the same the C code which actually does something very close, so I called it the “goes to operator”.

    Sorry for any misunderstanding it caused.

    Guy

    25 Mar 08 at 16:20

  5. Looks smart :-) I want to make use of it

    Viet

    6 Jul 08 at 16:36

Leave a Reply