Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Output of the simple question [modified]

Output of the simple question [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S shubhi

    Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.

    "We can't solve problems by using the same kind of thinking we used when we created them"

    modified on Monday, June 8, 2009 6:08 AM

    R Offline
    R Offline
    Rajesh R Subramanian
    wrote on last edited by
    #3

    int a = 1;

    a += ( a+= 3, 5, a );

    a += ( a+= 3 ); //(Anything after the first comma will be ignored by the compiler. See Sequential evaluation operator[^]

    a += ( a = a+3) //but a was delcared 1, so the expression a=1+3 makes RHS value as 4.

    a += a; //But a is 4 now before executing this statement.
    //Therefore adding another 4, the resultant a = 8. Makes perfect sense to me.

    shubhi wrote:

    In AS/400’s C showing output: 5

    Then that compiler sucks.

    It is a crappy thing, but it's life -^ Carlo Pallini

    S 1 Reply Last reply
    0
    • CPalliniC CPallini

      IMHO VC++ answer makes sense.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #4

      It's a correct answer, but maybe not what was intended. It seems to be evaluating it as ((a+=3), 5, a) and returning the new value of 'a' (the AS/400 compiler returns the old value of 'a' I think). I'll have to find a precedence table, as I'd have expected it to be (a += (3, 5, a)), which would give a different result (4)... edit : looks like the assignment has higher precedence, according to MSDN. You learn something new every day :)

      There are three kinds of people in the world - those who can count and those who can't...

      1 Reply Last reply
      0
      • R Rajesh R Subramanian

        int a = 1;

        a += ( a+= 3, 5, a );

        a += ( a+= 3 ); //(Anything after the first comma will be ignored by the compiler. See Sequential evaluation operator[^]

        a += ( a = a+3) //but a was delcared 1, so the expression a=1+3 makes RHS value as 4.

        a += a; //But a is 4 now before executing this statement.
        //Therefore adding another 4, the resultant a = 8. Makes perfect sense to me.

        shubhi wrote:

        In AS/400’s C showing output: 5

        Then that compiler sucks.

        It is a crappy thing, but it's life -^ Carlo Pallini

        S Offline
        S Offline
        shubhi
        wrote on last edited by
        #5

        But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.

        "We can't solve problems by using the same kind of thinking we used when we created them"

        R 2 Replies Last reply
        0
        • S shubhi

          But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.

          "We can't solve problems by using the same kind of thinking we used when we created them"

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #6

          shubhi wrote:

          then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.

          You're right there.

          shubhi wrote:

          a += ( a+= 3, 5, a ); //a=1

          Yes, the value of a(the last within the braces) is 1, so adding 3 to it makes it 4. And then there is an a+=a. Therefore 8.

          It is a crappy thing, but it's life -^ Carlo Pallini

          modified on Monday, June 8, 2009 7:04 AM

          CPalliniC 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            shubhi wrote:

            then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.

            You're right there.

            shubhi wrote:

            a += ( a+= 3, 5, a ); //a=1

            Yes, the value of a(the last within the braces) is 1, so adding 3 to it makes it 4. And then there is an a+=a. Therefore 8.

            It is a crappy thing, but it's life -^ Carlo Pallini

            modified on Monday, June 8, 2009 7:04 AM

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #7

            Rajesh R Subramanian wrote:

            a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.

            I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            R A 2 Replies Last reply
            0
            • CPalliniC CPallini

              Rajesh R Subramanian wrote:

              a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.

              I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #8

              Yes, that was subtle indeed. Modified my answer. :-O

              It is a crappy thing, but it's life -^ Carlo Pallini

              1 Reply Last reply
              0
              • CPalliniC CPallini

                Rajesh R Subramanian wrote:

                a cannot get the value 2, because the expression b+=2 will be executed before it is assigned to a, for the very reason that it is inside braces. Therefore, the value of a would be b+2.

                I think you're wrong, here (and the OP question was indeed more subtle than I origianlly understood). :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                A Offline
                A Offline
                Anoop Dashora
                wrote on last edited by
                #9

                Just to add For eg we have e1 , e2 Here ',' act as operator which will ``evaluate the subexpression e1, then evaluate e2; the value of the expression is the value of e2. So for a+=(a+=3,5,a) it will have as:- a+=(4,5,4)// since a=a+3 So in all a=8. But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.

                CPalliniC 1 Reply Last reply
                0
                • S shubhi

                  But according to sequential Evaluation: say If, a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here. So i think in my question it is evaluating the a+=3 but finally it is resulting the value of last argument. a += ( a+= 3, 5, a ); //a=1 a += ( a );//a=4 kindly correct me know if I m wrong.

                  "We can't solve problems by using the same kind of thinking we used when we created them"

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #10

                  Hello, I modified a portion of my previous answer. To be specific,

                  shubhi wrote:

                  a=(b+=2,5,2) then it will evaluate b+=2 but a will get the value 2, as I m using the braces here.

                  That is correct. :)

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  • A Anoop Dashora

                    Just to add For eg we have e1 , e2 Here ',' act as operator which will ``evaluate the subexpression e1, then evaluate e2; the value of the expression is the value of e2. So for a+=(a+=3,5,a) it will have as:- a+=(4,5,4)// since a=a+3 So in all a=8. But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #11

                    Anoop Dashora wrote:

                    But not sure of AS/400 C Compiler. Might be it is evaluating as:- a+=(a+=3.5,a) a=a+(a=a+3,5,a) a=1+(4,5,4) a=5 // But i am not sure with this explanation.

                    You could generate the assembly and have a look at it, couldn't you? [added] Probably you can't, since you're not the OP... :-D [/added]

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • S shubhi

                      Hi, what would be the output of following code: main() { int a=1; printf ("%d", a+=(a+=3,5,a)); } In VC++ it is showing output: 8 In AS/400’s C showing output: 5 If it is complier dependent, plz tell me the logic of both the answers.

                      "We can't solve problems by using the same kind of thinking we used when we created them"

                      modified on Monday, June 8, 2009 6:08 AM

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #12

                      Who cares what the answer is or should be or whatever - the lesson you should be learning from this question is just don't do this sort of thing. [edit]To expand on that - whenever some part of a language specification is defined as "implementation specific" or (as I think the C++ Standard says for cases like yours) "undefined", especially when it's language semantics like this, be afraid, be very afraid, and leave it well alone, as far as you are able.[/edit]

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      modified on Monday, June 8, 2009 8:30 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups