0

It feels like a really silly question but i just saw some line of code here that ended with a comma ',' and XCode did not complain about an expected ';'

So i fooled around:

    float a = 1.0;
    float b = 2.0;
    float c = 3.0;

    a = b, //comma here
    c = 4.0;

    NSLog(@"%f %f %f",a,b,c);

The result is log is correct, but I am wondering if there is a difference as I sometimes (rarely) seem to miss the shift key when typing a semicolon.

I have read this Use of commas versus semicolons in JavaScript? but it is about javascript and i am not sure if it applies here as well.

Community
  • 1
  • 1
NikkyD
  • 2,209
  • 1
  • 16
  • 31
  • The link you give for JavaScript is almost precisely the same as for C, including caveats. (The only difference is there is no `var` is C, so replace that with `int` for the same meaning.) – Rob Napier Feb 19 '16 at 19:06

1 Answers1

1

It's the C comma operator. It evaluates both operands and returns the result of the second, although in the case of a = b, c = 4.0 it sets a equal to b and c equal to 4.0.

mipadi
  • 398,885
  • 90
  • 523
  • 479