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. Can someone give a detail of this sample coder why the result is 9,and not 4??

Can someone give a detail of this sample coder why the result is 9,and not 4??

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 7 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.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    #include <iostream.h>
    int main()
    {
    int x=1,y;
    y= ++x*++x;
    cout << x << endl;
    cout << y <<endl;
    return 0;
    }

    H C L I 5 Replies Last reply
    0
    • W wbgxx

      #include <iostream.h>
      int main()
      {
      int x=1,y;
      y= ++x*++x;
      cout << x << endl;
      cout << y <<endl;
      return 0;
      }

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Because you shouldn't use the pre-increment operator that way. :)

      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]

      1 Reply Last reply
      0
      • W wbgxx

        #include <iostream.h>
        int main()
        {
        int x=1,y;
        y= ++x*++x;
        cout << x << endl;
        cout << y <<endl;
        return 0;
        }

        H Offline
        H Offline
        hasani2007
        wrote on last edited by
        #3

        Hello It's not very hard. answer of this simple question is : YOU WRITE ++x TWICE.y=++x[first time]*++x[second time] don't forget to vote me. BYE...

        1 Reply Last reply
        0
        • W wbgxx

          #include <iostream.h>
          int main()
          {
          int x=1,y;
          y= ++x*++x;
          cout << x << endl;
          cout << y <<endl;
          return 0;
          }

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

          ++x // x now = 2
          * // will multiply in a moment
          ++x // x now = 3
          // now multiply x(3) by x(3) = 9

          You should not use expressions like this as they may not give the results you expect.

          It's time for a new signature.

          R 1 Reply Last reply
          0
          • W wbgxx

            #include <iostream.h>
            int main()
            {
            int x=1,y;
            y= ++x*++x;
            cout << x << endl;
            cout << y <<endl;
            return 0;
            }

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

            Note that priority of ++ operation is higher than *.If you have y= ++x*x++ it should be 4. I would recommend you not to use such features because it makes code very hard to understand and modify. Keep it simple and you will avoid getting in troubles. :)

            Life is a stage and we are all actors!

            L 1 Reply Last reply
            0
            • L Lost User

              ++x // x now = 2
              * // will multiply in a moment
              ++x // x now = 3
              // now multiply x(3) by x(3) = 9

              You should not use expressions like this as they may not give the results you expect.

              It's time for a new signature.

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

              Richard MacCutchan wrote:

              You should not use expressions like this as they may not give the results you expect.

              I welcome you to take a look at our codebase. :laugh:

              Workout progress:
              Current arm size: 14.4in
              Desired arm size: 18in
              Next Target: 15.4in by Dec 2010

              Current training method: HIT

              1 Reply Last reply
              0
              • L Lost User

                Note that priority of ++ operation is higher than *.If you have y= ++x*x++ it should be 4. I would recommend you not to use such features because it makes code very hard to understand and modify. Keep it simple and you will avoid getting in troubles. :)

                Life is a stage and we are all actors!

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hristo Bojilov wrote:

                Keep it simple

                I agree. BTW: the exact order of the expression evaluation and the side-effects has been undefined in many languages for a long time. However IMO a compiler should issue a warning whenever you rely on assumptions, i.e. undefined results. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                1 Reply Last reply
                0
                • W wbgxx

                  #include <iostream.h>
                  int main()
                  {
                  int x=1,y;
                  y= ++x*++x;
                  cout << x << endl;
                  cout << y <<endl;
                  return 0;
                  }

                  I Offline
                  I Offline
                  Iain Clarke Warrior Programmer
                  wrote on last edited by
                  #8

                  This is basically the same question as: http://www.codeproject.com/Messages/3507402/A-Doubt.aspx[^] Where in your class does your colleague sit? I expect you both to get high marks now! Iain.

                  I am one of "those foreigners coming over here and stealing our jobs". Yay me!

                  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