CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Prefix and Postfix ++ and - -

    I just have some questions about the prefix and postfix versions of the ++ and -- statement. Correct me if I'm wrong but when the postfix ++ or -- statement it used it increments (or decrements) the variable it's connected to but returns the value of the variable directly before the ++ or -- statement was called so then
    int i=5;
    a = i++
    would make a = 5 and i = 6;
    When the prefix versions are used it increments or decrements depending on which is called the connected variable. But this time it returns the new value so if the prefix versioin was used above a would = 6. Now here's my question I have the following code in a program
    int i=5;
    c = (i++) + (i++);
    and it makes c = 10, not 11. Why is that?



  2. #2
    Join Date
    May 1999
    Location
    Texas, USA
    Posts
    568

    Re: Prefix and Postfix ++ and - -

    It seems like it would be doesn't it. But if you examine the assembly code this is what the following code would do.

    int i = 5;

    c = (i++) + (i++);

    1) i is stored in register.
    2) the register is added to the same register and stored in c.
    3) i is incremented.

    Makes sense huh,

    Wayne


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured