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. A Doubt

A Doubt

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
12 Posts 8 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.
  • B Offline
    B Offline
    Benjamin Bruno
    wrote on last edited by
    #1

    int i =2; int j = ++i + ++i; cout<<" value of j is : " << j; in the above code the output value of j is 8,I am confused about that.Please help?

    N L S T 4 Replies Last reply
    0
    • B Benjamin Bruno

      int i =2; int j = ++i + ++i; cout<<" value of j is : " << j; in the above code the output value of j is 8,I am confused about that.Please help?

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      No need to be confused ++i => i = 3 ++i => i = 4 + => 4+4 = 8

      home

      C 1 Reply Last reply
      0
      • N Niklas L

        No need to be confused ++i => i = 3 ++i => i = 4 + => 4+4 = 8

        home

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        where does the 2nd '4' come from, in your bottom line ?

        image processing toolkits | batch image processing

        N 1 Reply Last reply
        0
        • C Chris Losinger

          where does the 2nd '4' come from, in your bottom line ?

          image processing toolkits | batch image processing

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #4

          There is only one instance of i, so i will be incremented twice before the addition occurs.

          home

          C A 2 Replies Last reply
          0
          • N Niklas L

            There is only one instance of i, so i will be incremented twice before the addition occurs.

            home

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            :-O

            image processing toolkits | batch image processing

            1 Reply Last reply
            0
            • N Niklas L

              There is only one instance of i, so i will be incremented twice before the addition occurs.

              home

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              Just playing devil's advocate... But you're not adding i twice, you're adding the results of the expression ++i which is either going to be 3 or 4 depending on how many times you've done the ++ operation. Cheers, Ash

              N 1 Reply Last reply
              0
              • A Aescleal

                Just playing devil's advocate... But you're not adding i twice, you're adding the results of the expression ++i which is either going to be 3 or 4 depending on how many times you've done the ++ operation. Cheers, Ash

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #7

                ...but if you add 3 and 4 you will not get 8, and the OP would not have been confused in the first place. It would be a shame if once in a while you didn't get an unexpected result. (The testing department might disagree on that though)

                home

                1 Reply Last reply
                0
                • B Benjamin Bruno

                  int i =2; int j = ++i + ++i; cout<<" value of j is : " << j; in the above code the output value of j is 8,I am confused about that.Please help?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  This is a common issue and the rules are: don't do this as the results are not guaranteed. Whatever answer you get you may get a different answer with another manufacturer's compiler. If you really need to evaluate such an expression you need to break it into steps like:

                  int i = 2;
                  int j = ++i;
                  j += ++i;

                  It's time for a new signature.

                  1 Reply Last reply
                  0
                  • B Benjamin Bruno

                    int i =2; int j = ++i + ++i; cout<<" value of j is : " << j; in the above code the output value of j is 8,I am confused about that.Please help?

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #9

                    It's pretty subtle. Here's a breakdown of what's hapenning:

                    1. ++i increments i (so i=3) and returns a reference to i.
                    2. The second ++i increments i (so i=4) and returns a reference to i.
                    3. The + is called with the two references. Both are dereferenced and added (4+4).

                    Steve

                    J 1 Reply Last reply
                    0
                    • B Benjamin Bruno

                      int i =2; int j = ++i + ++i; cout<<" value of j is : " << j; in the above code the output value of j is 8,I am confused about that.Please help?

                      T Offline
                      T Offline
                      Tim Craig
                      wrote on last edited by
                      #10

                      To reinforce what Richard said, while this is cute, it's always counterproductive. It's so subtle that even if you KNOW what you're doing, you're going to screw it up and in a production environment, you're going to confuse someone who's not was good with obfurscated code as you think you are. Just write it out in the clearest possible terms and let the compiler worry about optimizing it these days. The compiler can do optimizations that would curl your hair if you looked at what it was doing. :)

                      Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        It's pretty subtle. Here's a breakdown of what's hapenning:

                        1. ++i increments i (so i=3) and returns a reference to i.
                        2. The second ++i increments i (so i=4) and returns a reference to i.
                        3. The + is called with the two references. Both are dereferenced and added (4+4).

                        Steve

                        J Offline
                        J Offline
                        JobinG
                        wrote on last edited by
                        #11

                        int i = 2; int j = ++i + i++ + ++i + i++ + ++i; cout<<"Value of j: "<<j; The value getting for 'j' is 19. As you have said the three ++i statements will execute and the value of i become 5. How will execute the i++ statements? Could you please explain.

                        S 1 Reply Last reply
                        0
                        • J JobinG

                          int i = 2; int j = ++i + i++ + ++i + i++ + ++i; cout<<"Value of j: "<<j; The value getting for 'j' is 19. As you have said the three ++i statements will execute and the value of i become 5. How will execute the i++ statements? Could you please explain.

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #12

                          Firstly there are two cases here, the pre-increment and (++i) the post-increment (i++). The pre-increment case increments the variable and returns a reference to it. The post-increment case copies the variable, increments the original and returns the copy.

                          Steve

                          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