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

This is a discussion on x = (y = 3, y + 1) why 3 is assigned to y first before calculating y + 1 within the C Programming forums, part of the General Programming Boards category; Consider this simple program: Code: main() { int x, y; x = (y = 3, y + 1); printf("x = ...

  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
    21,451
    I might be wrong, but from what I understand the comma is a sequence point, so y = 3 is evaluated before y + 1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    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, 12:03 PM
  3. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 06: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

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