Simultaneously Update Two Variables

This is a discussion on Simultaneously Update Two Variables within the C Programming forums, part of the General Programming Boards category; Is there a way to simultaneously update two variables, like in Python? For example Code: x, y = 0, 1 ...

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    18

    Simultaneously Update Two Variables

    Is there a way to simultaneously update two variables, like in Python?

    For example

    Code:
    x, y = 0, 1
    x, y = y, x + y

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    Nope, just write them on two lines:
    Code:
    x = 0;
    y = 1;
    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.

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Hmm, you could do:

    x = 0, y = 1;
    t = y, y += x, x = t;

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,496
    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.
    Quote Originally Posted by Barney McGrew View Post
    Hmm, you could do:

    x = 0, y = 1;
    t = y, y += x, x = t;
    By the same argument, I could just do
    Code:
    x = 0; y = 1;
    t = y; y += x; x = t;
    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).

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling same function simultaneously
    By xeratule in forum C Programming
    Replies: 12
    Last Post: 05-05-2011, 08:45 AM
  2. how to run two functions simultaneously ??
    By paisat007 in forum C Programming
    Replies: 1
    Last Post: 10-18-2010, 12:10 PM
  3. Running 2 functions simultaneously
    By blkhockeypro19 in forum C Programming
    Replies: 7
    Last Post: 04-19-2009, 12:48 AM
  4. using -ggdb3 and -O3 simultaneously for gcc
    By lehe in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2009, 04:22 PM
  5. how to execute in two C programs simultaneously.
    By Albert franklin in forum C Programming
    Replies: 2
    Last Post: 08-04-2004, 06:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21