Thread: c=(a=40,b=50) , how output c=50

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    c=(a=40,b=50) , how output c=50

    Code:
    int main()
    {
    int a=10,b=20,c=30;
    c=(a=40,b=50);
    printf("%d %d %d \n",a,b,c);
    return 0;
    }
    out put : 40 50 50

    can any one explain me how this out came , how c assign to 50

    i appreciate any replys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The result of the comma operator is the right subexpression. Where did you find this program, anyway?
    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
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Hi laserlight ,

    "The result of the comma operator is the right subexpression" , i didt get this . if u dont mine can u explain me clearlly . can u give any example for this right subexpression

    i facedout this in oneof my c interview .

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here is a simple example that demonstrates what I mean:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = (10, 20);
        printf("%d\n", x);
        return 0;
    }
    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

  5. #5
    Learning C. JOZZY& Wakko's Avatar
    Join Date
    Nov 2009
    Posts
    59
    Quote Originally Posted by nkrao123@gmail. View Post
    Code:
    int main()
    {
    int a=10,b=20,c=30;
    c=(a=40,b=50);
    printf("%d %d %d \n",a,b,c);
    return 0;
    }
    A takes on the value 40 in line 4.
    B takes on the value 50 in line 4.
    The value of variable B is stored in C at line 4.

    Read this Comma operator - Wikipedia, the free encyclopedia
    Code:
    i = (a, b, c);          // stores c into i
    If you would like the value of A stored in C you would edit it to.
    Code:
    c = a=40, b=30
    Last edited by JOZZY& Wakko; 12-17-2009 at 06:42 AM.

  6. #6
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Wow, I feel like a total chode. I could've sworn that the result of an assignment operation was either a 1 or a 0.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Epy View Post
    Wow, I feel like a total chode. I could've sworn that the result of an assignment operation was either a 1 or a 0.
    Don't confuse the equality operator "==" with the assignment operator "=".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM