Can someone tell me why? (Unary pre and post increment operator

Discussion in 'C++' started by Andreas Sheriff, Sep 24, 2004.

  1. Please evaluate the following snippet:

    int a=5;
    int b;
    b = a++ + a;
    Edit1->Text = b;
    a = 5;
    b = a++ + ++a;
    Edit2->Text = b;

    Can you tell me why the value of b assigned to Edit1->Text is 10
    while the value of b assigned to Edit2->Text is 12?

    And if you can, why isn't it documented?

    --
    WARNING:
    DO NOT REPLY TO THIS EMAIL
    Reply to me only on this newsgroup
    Andreas Sheriff, Sep 24, 2004
    #1
    1. Advertising

  2. Andreas Sheriff

    JKop Guest

    Andreas Sheriff posted:

    > Please evaluate the following snippet:
    >
    > int a=5;


    Defines a variable called "a" of the type "int" and sets its value to 5.

    > int b;


    Defines variable: Name = "b". Type = "int". Value = no particular value

    > b = a++ + a;


    "b" could end up with two possible values here, either:

    10

    or

    11

    as the Standard doesn't dictate whether "a++" or "a" will be done first.

    The variable "a" ends up with the value 6.

    > Edit1->Text = b;



    The text will be either 10 or 11.


    > a = 5;



    Sets "a"'s value to 5.


    > b = a++ + ++a;



    "b" gets:

    12

    either by "5 + 7" or "6 + 6".

    "a" becomes 7.


    > Edit2->Text = b;



    Text is set to 12.


    > Can you tell me why the value of b assigned to Edit1->Text is 10
    > while the value of b assigned to Edit2->Text is 12?



    You're welcome!


    -JKop
    JKop, Sep 24, 2004
    #2
    1. Advertising

  3. Andreas Sheriff

    PKH Guest

    "Andreas Sheriff" <> wrote in message
    news:noQ4d.289519$Lj.229505@fed1read03...
    > Please evaluate the following snippet:
    >
    > int a=5;
    > int b;
    > b = a++ + a;
    > Edit1->Text = b;
    > a = 5;
    > b = a++ + ++a;
    > Edit2->Text = b;
    >
    > Can you tell me why the value of b assigned to Edit1->Text is 10
    > while the value of b assigned to Edit2->Text is 12?
    >
    > And if you can, why isn't it documented?
    >
    > --
    > WARNING:
    > DO NOT REPLY TO THIS EMAIL
    > Reply to me only on this newsgroup
    >


    Here's what I think happens:

    > int a=5;
    > int b;
    > b = a++ + a;

    1) b = 5 + 5
    2) a++ is done after the assignment, a = 6

    > Edit1->Text = b;
    > a = 5;
    > b = a++ + ++a;

    1) ++a is done before the assignment, a = 6
    2) b = 6 + 6
    3) a++ is done after the assignment, a = 7

    > Edit2->Text = b;


    PKH
    PKH, Sep 24, 2004
    #3
  4. Andreas Sheriff

    Rolf Magnus Guest

    JKop wrote:

    > Andreas Sheriff posted:
    >
    >> Please evaluate the following snippet:
    >>
    >> int a=5;

    >
    > Defines a variable called "a" of the type "int" and sets its value to 5.
    >
    >> int b;

    >
    > Defines variable: Name = "b". Type = "int". Value = no particular value
    >
    >> b = a++ + a;

    >
    > "b" could end up with two possible values here, either:
    >
    > 10
    >
    > or
    >
    > 11


    Those are not the only possible results. a is read from and written to
    without a sequence point in between. The behavior of that line is
    undefined, so anything can happen. From the point of undefined behavior on,
    it's of no use to discuss what any following code does.
    Rolf Magnus, Sep 24, 2004
    #4
  5. Andreas Sheriff

    Tom Widmer Guest

    On Fri, 24 Sep 2004 00:53:55 -0700, "Andreas Sheriff"
    <> wrote:

    >Please evaluate the following snippet:
    >
    > int a=5;
    > int b;
    > b = a++ + a;


    The above both modifies a (a++) and separately reads its value (a).

    > Edit1->Text = b;
    > a = 5;
    > b = a++ + ++a;


    The above modifies a twice.

    > Edit2->Text = b;
    >
    >Can you tell me why the value of b assigned to Edit1->Text is 10
    >while the value of b assigned to Edit2->Text is 12?
    >
    >And if you can, why isn't it documented?


    Modifying a value more than once or modifying it and reading it except
    as part of the modification without an intervening sequence point
    results in undefined behaviour. So the program might generate any
    result at all for the two lines in question, or might crash. Have a
    look at the C faq on expressions:
    http://www.eskimo.com/~scs/C-faq/s3.html

    Tom
    Tom Widmer, Sep 24, 2004
    #5
  6. Andreas Sheriff

    nebjy Guest

    Andreas Sheriff wrote:

    > Please evaluate the following snippet:
    >
    > int a=5;
    > int b;
    > b = a++ + a;
    > Edit1->Text = b;
    > a = 5;
    > b = a++ + ++a;
    > Edit2->Text = b;
    >
    > Can you tell me why the value of b assigned to Edit1->Text is 10
    > while the value of b assigned to Edit2->Text is 12?
    >
    > And if you can, why isn't it documented?
    >


    It's a very interesting situation. It looks like b is being evaluated
    from the right, leftwards. So in the first case, a=5, added to a++ which
    returns 5 and then incremented (as opposed to ++a which increments first
    and then returns that). So in the second case, 6 is returned which is
    added to another 6 (which is then incremented but that makes no
    difference). Giving 12.

    However!

    If you genuinely have code like this in your program, then you should
    seriously consider rewriting it with a few more lines, for everyone's
    sanity :)
    nebjy, Sep 24, 2004
    #6
  7. nebjy <> wrote:
    > Andreas Sheriff wrote:
    >
    >> Please evaluate the following snippet:
    >>
    >> int a=5;
    >> int b;
    >> b = a++ + a;
    >> Edit1->Text = b;
    >> a = 5;
    >> b = a++ + ++a;
    >> Edit2->Text = b;
    >>
    >> Can you tell me why the value of b assigned to Edit1->Text is 10
    >> while the value of b assigned to Edit2->Text is 12?
    >>
    >> And if you can, why isn't it documented?
    >>

    >
    > It's a very interesting situation. It looks like b is being evaluated
    > from the right, leftwards. So in the first case, a=5, added to a++ which
    > returns 5 and then incremented (as opposed to ++a which increments first
    > and then returns that). So in the second case, 6 is returned which is
    > added to another 6 (which is then incremented but that makes no
    > difference). Giving 12.


    It looks like it is evaluated from left to right: Taking A (5) then
    incrementing it to 6. Afterward A is incremented to 7 and then added to the
    5, giving a result of 12.
    --
    Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
    Simon Stienen, Sep 24, 2004
    #7
  8. Andreas Sheriff

    nebjy Guest

    Simon Stienen wrote:

    > nebjy <> wrote:
    >
    >>Andreas Sheriff wrote:
    >>
    >>
    >>>Please evaluate the following snippet:
    >>>
    >>> int a=5;
    >>> int b;
    >>> b = a++ + a;
    >>> Edit1->Text = b;
    >>> a = 5;
    >>> b = a++ + ++a;
    >>> Edit2->Text = b;
    >>>
    >>>Can you tell me why the value of b assigned to Edit1->Text is 10
    >>>while the value of b assigned to Edit2->Text is 12?
    >>>
    >>>And if you can, why isn't it documented?
    >>>

    >>
    >>It's a very interesting situation. It looks like b is being evaluated
    >>from the right, leftwards. So in the first case, a=5, added to a++ which
    >>returns 5 and then incremented (as opposed to ++a which increments first
    >>and then returns that). So in the second case, 6 is returned which is
    >>added to another 6 (which is then incremented but that makes no
    >>difference). Giving 12.

    >
    >
    > It looks like it is evaluated from left to right: Taking A (5) then
    > incrementing it to 6. Afterward A is incremented to 7 and then added to the
    > 5, giving a result of 12.


    True, but that doesn't hold for the first case. Evaluating from left to
    right there gives 11, not 10 as the OP said.
    nebjy, Sep 24, 2004
    #8
  9. nebjy <> wrote:
    > Simon Stienen wrote:
    >
    >> nebjy <> wrote:
    >>
    >>>Andreas Sheriff wrote:
    >>>
    >>>
    >>>>Please evaluate the following snippet:
    >>>>
    >>>> int a=5;
    >>>> int b;
    >>>> b = a++ + a;
    >>>> Edit1->Text = b;
    >>>> a = 5;
    >>>> b = a++ + ++a;
    >>>> Edit2->Text = b;
    >>>>
    >>>>Can you tell me why the value of b assigned to Edit1->Text is 10
    >>>>while the value of b assigned to Edit2->Text is 12?
    >>>>
    >>>>And if you can, why isn't it documented?
    >>>>
    >>>
    >>>It's a very interesting situation. It looks like b is being evaluated
    >>>from the right, leftwards. So in the first case, a=5, added to a++ which
    >>>returns 5 and then incremented (as opposed to ++a which increments first
    >>>and then returns that). So in the second case, 6 is returned which is
    >>>added to another 6 (which is then incremented but that makes no
    >>>difference). Giving 12.

    >>
    >>
    >> It looks like it is evaluated from left to right: Taking A (5) then
    >> incrementing it to 6. Afterward A is incremented to 7 and then added to the
    >> 5, giving a result of 12.

    >
    > True, but that doesn't hold for the first case. Evaluating from left to
    > right there gives 11, not 10 as the OP said.


    Ok, you won... I didn't think THAT far, sorry :S
    --
    Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
    »What you do in this world is a matter of no consequence,
    The question is, what can you make people believe that you have done.«
    -- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
    Simon Stienen, Sep 24, 2004
    #9
  10. Andreas Sheriff wrote:
    > Please evaluate the following snippet:
    >
    > int a=5;
    > int b;
    > b = a++ + a;
    > Edit1->Text = b;
    > a = 5;
    > b = a++ + ++a;
    > Edit2->Text = b;
    >
    > Can you tell me why the value of b assigned to Edit1->Text is 10
    > while the value of b assigned to Edit2->Text is 12?


    Both 'a++ + a' expression and 'a++ + ++a' expression produce undefined
    behavior. The former modifies 'a' and reads it value for unrelated
    purpose. The latter modifies 'a' twice. Both violate requirements
    specified in 5/4.

    There's no way to predict what is assigned to 'b', if anything is
    assigned at all. Your code is broken.

    > And if you can, why isn't it documented?


    There's no point to document it more than it already is documented: the
    code produces undefined behavior. End of story.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Sep 24, 2004
    #10
  11. Andreas Sheriff

    Jack Klein Guest

    On Fri, 24 Sep 2004 11:48:51 +0200, "PKH" <>
    wrote in comp.lang.c++:

    >
    > "Andreas Sheriff" <> wrote in message
    > news:noQ4d.289519$Lj.229505@fed1read03...
    > > Please evaluate the following snippet:
    > >
    > > int a=5;
    > > int b;
    > > b = a++ + a;
    > > Edit1->Text = b;
    > > a = 5;
    > > b = a++ + ++a;
    > > Edit2->Text = b;
    > >
    > > Can you tell me why the value of b assigned to Edit1->Text is 10
    > > while the value of b assigned to Edit2->Text is 12?
    > >
    > > And if you can, why isn't it documented?
    > >
    > > --
    > > WARNING:
    > > DO NOT REPLY TO THIS EMAIL
    > > Reply to me only on this newsgroup
    > >

    >
    > Here's what I think happens:


    Here's what I know happens. The behavior is specifically undefined by
    the C++ language. Whatever does or does not happen is just as right
    or wrong as anything else. The C++ language takes no responsibility
    for the results.

    The results are off-topic here because they are not a language issue.
    They are, at best, a compiler-specific issue.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 25, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    44,953
    gene tani
    Jan 13, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    8
    Views:
    917
    Alf P. Steinbach
    Nov 22, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    880
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    508
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page