Meaning of this statement?

This is a discussion on Meaning of this statement? within the C Programming forums, part of the General Programming Boards category; I just compiled successfully what I thought was an invalid statement... now I'm left wondering what the heck it means ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Meaning of this statement?

    I just compiled successfully what I thought was an invalid statement... now I'm left wondering what the heck it means since I've never seen such a statement in C before. It would appear to be some sort of equality statement... Here it is:
    Code:
    if((handle1, "Some String", "Title", int1 | int2 | int3)== int4)
    I would guess it takes on the form "(term1, term2, term3, ...)" but what does it mean (or evaluate to) when used in an equality expression?

    p.s. if this compares each value seperated by commas with the int4 value... then I'm wondering why I haven't learned about this shortcut earlier!!!

    (if this is some super-common statement in C that I just somehow didn't come across before, then I apologize for being a newbie )

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    I don't see how that would do anything. The comma operator is basically just used to create an evaluation list in a statement. For instance in a for statement, you could do:
    Code:
    for(i = 0, j = 10; i < j; ++i, --j)
    This would do as you expected. Assign 0 to i and 10 to j and loop incrementing i and decrementing j until i => j.

    The comma operator, I believe returns the result. Which, unless you put it somewhere, gets discarded. In the case of your statement above, it should act no differently than
    Code:
    if(int1 | int2 | int3 == int4)
    You can test it yourself
    Code:
        if((1,2) == 3)
          std::cout << "if((1,2) == 3) is True!";
        if((1,2,3) == 3)
          std::cout << "if((1,2,3) == 3) is True!";
        if((1,2,3,4) == 3)
          std::cout << "if((1,2,3,4) == 3) is True!";
    I could be wrong, though. There might be something else going on there that I'm not seeing. I don't think so, though.
    Last edited by SlyMaelstrom; 07-14-2006 at 03:41 AM.
    Sent from my iPad«

  3. #3
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    The program goes through the whole list and returns the last element and since all the statements were useless it's the same as
    Code:
    if (int1 | int2 | int3 == int4)

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Indeed you are right! Only the last value is actually used in comparison.
    I knew about the comma's use in the for statement, but I had no idea that it could be used in a similar capacity in all equality statements.... and to think I almost thought it might be a shorthand for comparing many values against a single value... too good to be true :P

    Thanks for clearing that up for me!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    The comma operator, I believe returns the result. Which, unless you put it somewhere, gets discarded. In the case of your statement above, it should act no differently than
    Code:
    if(int3 == int4)
    You seem to have overlooked something...


    Quazh.
    Hope is the first step on the road to disappointment.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Ah, yes, I got so caught up in his question about the comma that I did infact overlook the bitwise operator. I shall edit my post.
    Sent from my iPad«

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    The comma operator,bit wise OR operator(|) have left to right associativity meaning that

    if

    a=r(x,y,...,z)
    is equivalent to
    a=z; after all the statement x,y,... are executed


    check this link

    http://h30097.www3.hp.com/docs/base_...E/DOCU_059.HTM

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not exactly sure what your point is here. The oring takes place regardless of the comma operator. Thus, you should be saying:
    Code:
    a = w, x, y | z;
    Is the same as:
    Code:
    a = y | z;
    Ignore the warnings, since we're talking about crappy code originally, as this is a further illustration of said crappy code:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int a = (1, 2, 3);
        int b = (0, 1 | 2 | 4);
        int c = (1 | 2 | 4, 0);
    
        printf( "a is %d, b is %d, c is %d\n", a, b, c );
    
        if( (a,c,b) == (1 | 2 | 4) )
            printf( "b is 7\n" );
        
        return 0;
    }
    
    /*
    a is 3, b is 7, c is 0
    b is 7
    */
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the meaning of this while statement..
    By transgalactic2 in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 02:52 PM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 06:18 AM
  4. The Meaning of Life: A Trick Question?
    By chix/w/guns in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 07-12-2004, 07:53 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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