comma operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    comma operator

    int i,j=10,k=11;
    i=j,k;//here i=j
    i=(j,k);//here i=k
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Do you have a question?

    Comment

    • destination
      New Member
      • Dec 2009
      • 34

      #3
      actually i forgot to type my query.
      ya my question is regarding assignment in the two expression.
      in the second expression why is i=k. why cant it be like this i=j.
      thanx

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Have you tried testing these out in code to see what happens?

        Comment

        • destination
          New Member
          • Dec 2009
          • 34

          #5
          ya i tried both code. i needed the reason for two assignment.

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Have you tried Google?

            I suggest you search for "c++ assignment operator" and read the articles.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              This is related to both operator precedence and the value returned by the comma operator.

              You could easily look both of these up in which case you would be able to tell what the value of

              l = (i=j,k);

              l (assuming it is another int) is in this expression.

              What most people forget is how the comma operator works, it has the syntax

              <expression1>,< expression2>

              but it returns the value <expression2> . Now at this point you are probably about to ask if it returns the value <expression2> then how can i = j in the statement

              i=j,k;

              surely i should equal k if the comma operator is returning <expression2> . However both = and , are operators so the order they get evaluated in in C is dependent on their relative precedence. Looking at an operator precedence table you will quickly see that the comma operator has the lowest precedence of any operator, that is everything takes precedence over it which means that

              i=j,k;

              is evaluated as

              (i=j),k;

              which is why i = j following the statement. So coming back to my original question what is the value of l following

              l = (i=j,k);

              We know that i = j because of the relative operator precedence of = and , I have surrounded the expression in ( ) to force it to evaluate first (i.e. break the precedence order which is what ( and ) are for) l takes on the value of the expression

              i=j,k

              which we know is the second expression so l = k.

              To be honest I have never come across what I would call a good use for the comma operator except in code obfuscation contests.

              Comment

              • RedSon
                Recognized Expert Expert
                • Jan 2007
                • 4980

                #8
                Shoot!

                I had a big counter argument for why Banfa was wrong complete with research and links and whatnot and then I re-read the post and my argument evaporated. I was going to say: Oh yea, well then how do you explain the right-to-left associativity of the assignment operator in the expression
                a = b = c = d = 1; since it is interpreted as (a = (b = (c = (d = 1))));
                But then I noticed that you are talking about the comma between the right hand operands as in i = j,k;
                I'm such a dunce some times :(

                Comment

                Working...