Thread: x = (y = 3, y + 1) why 3 is assigned to y first before calculating y + 1

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    x = (y = 3, y + 1) why 3 is assigned to y first before calculating y + 1

    Consider this simple program:

    Code:
    main()
    {
       int x, y;
       x = (y = 3, y + 1);
       printf("x = %d and y = %d", x, y);
    }
    It shows the output "x = 4 and y = 3";

    I just want to know why it first of all assigns the value 3 to y instead of calculating y + 1 as "+" has a higher priority as compared to "=".

    Thanks a lot.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I might be wrong, but from what I understand the comma is a sequence point, so y = 3 is evaluated before y + 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's because the comma operator "associates left to right", so the left part is performed before the right part. See:
    http://www.cppreference.com/operator_precedence.html

    [Since the comma operator is at the bottom of the precedence list, you may have to scroll down to find it].

    So, the left half is evaluated first (y = 3), then the right half (y + 1). And the final result is assigned to the x variable.

    Of course, if you did the "y + 1" first, it would have a undefined value.

    Edit: Fix up so that my reply applies to both the original post and Laserligth's answer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by hitesh_best View Post
    I just want to know why it first of all assigns the value 3 to y instead of calculating y + 1 as "+" has a higher priority as compared to "=".
    But comma itself has a priority much lower than either of those operators, so it splits the expression into self-contained portions. Because comma is also a sequence point, the compiler evaluates the left portion first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating : high numbers
    By MiraX33 in forum C++ Programming
    Replies: 9
    Last Post: 06-08-2006, 11:08 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. Taking input while calculating
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 04:47 PM