Is there a way to simultaneously update two variables, like in Python?
For example
Code:x, y = 0, 1 x, y = y, x + y
Is there a way to simultaneously update two variables, like in Python?
For example
Code:x, y = 0, 1 x, y = y, x + y
Nope, just write them on two lines:
On a related note, to save you the possible question, there is no way to return multiple variables/values from a function either. That is, unless they're bundled in a struct or something, in which case you're still really only returning one thing (the struct), not multiple, individual items.Code:x = 0; y = 1;
Hmm, you could do:
x = 0, y = 1;
t = y, y += x, x = t;
Note, the following is not to "knock" your suggestion, merely point out some stuff for the OP, since they appear to be new to C.
By the same argument, I could just do
The one-line aspect is just a matter of white space. Using the comma operator doesn't offer anything that can't be achieved with a semicolon, especially since the comma operator introduces a sequence point (without which you would open the door for issues of evaluation order and undefined behavior).Code:x = 0; y = 1; t = y; y += x; x = t;
The only useful case for the comma operator that comes to mind is when you need multiple initialization or increment expressions in a for loop. Any other uses just seem like syntactic sugar that does little to nothing to improve the code, it's readability, etc.
EDIT: Just Googled and found this: Comma operator - Wikipedia, the free encyclopedia. That case might be useful. All the others have, IMO, better alternatives.