Guess the bug!
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. ☺
March 31st, 2007 at 15:58:43
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”.
March 31st, 2007 at 16:25:27
Heh, you’re right. I thought that the comma would take precedence, and a would get 100. Silly me. :)
March 31st, 2007 at 18:05:27
I’m pretty nothing – not even funny thing here which has very low precedence – is lower on the order of events than a comma. :)
March 31st, 2007 at 18:05:58
s/pretty/pretty sure/
Though I’m also pretty, I’m not pretty nothing.
April 1st, 2007 at 11:29:52
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.