Guess the bug!

2007-03-31 15:44:30 -08:00

UPDATE: NO BUG! Serves me right for not testing a programming challenge before posting it. Thanks, Evan.

a = 42, b = 100;

Before you type “error on line 1”: No, it’s not a compilation error. The above code is legal, just wrong. The task before you is to explain how. ☺

5 Responses to “Guess the bug!”

  1. Evan Schoenberg Says:

    What were you trying to do? If all you want to do is assign 42 to ‘a’ and 100 to ‘b’, it’s fine, though weird, isn’t it? Now, if you wanted to check the return value, you’d find that the second statement’s return value was relevant, so:
    if ((a = 42, b = 100) == 42) {
    NSLog(@”a”);
    } else if ((a = 42, b = 100) == 100) {
    NSLog(@”b”);
    } else {
    NSLog(@”%i”,(a = 42, b = 100));
    }
    will output “b”.

  2. Peter Hosey Says:

    Heh, you’re right. I thought that the comma would take precedence, and a would get 100. Silly me. :)

  3. Evan Schoenberg Says:

    I’m pretty nothing – not even funny thing here which has very low precedence – is lower on the order of events than a comma. :)

  4. Evan Schoenberg Says:

    s/pretty/pretty sure/
    Though I’m also pretty, I’m not pretty nothing.

  5. Chuck Says:

    And this, children, is why we should never use the comma operator. It is a siren trying to lure you in with her arcane, 1337-looking song.

Leave a Reply

Do not delete the second sentence.


Warning: Undefined array key "ntt_saved_comment_text" in /home/public/blog/wp-content/plugins/negative-turing-test/negative-turing-test.php on line 143