opertor ++

Discussion in 'C++' started by ravinder thakur, May 17, 2012.

  1. hello guys,

    I am apparently puzzled by a very simple piece of code. What should be
    the value of x after this statement ?

    int x = 10;
    x = x++;

    I expect the value to be 10(since this is postfix). However the value
    turns out to be 11. Any idea whats happening ?

    Thanks
    xoxo
    ravinder thakur, May 17, 2012
    #1
    1. Advertising

  2. ravinder thakur

    red floyd Guest

    On 5/16/2012 10:20 PM, ravinder thakur wrote:
    > hello guys,
    >
    > I am apparently puzzled by a very simple piece of code. What should be
    > the value of x after this statement ?
    >
    > int x = 10;
    > x = x++;
    >
    > I expect the value to be 10(since this is postfix). However the value
    > turns out to be 11. Any idea whats happening ?
    >


    The behavior is undefined.
    red floyd, May 17, 2012
    #2
    1. Advertising

  3. ravinder thakur

    Senggen Choi Guest

    On Thursday, May 17, 2012 1:20:11 PM UTC+8, ravinder thakur wrote:
    > hello guys,
    >
    > I am apparently puzzled by a very simple piece of code. What should be
    > the value of x after this statement ?
    >
    > int x = 10;
    > x = x++;
    >
    > I expect the value to be 10(since this is postfix). However the value
    > turns out to be 11. Any idea whats happening ?
    >
    > Thanks
    > xoxo


    You need another variable.

    int y = x++;

    then y should be 10 as you expected, and x equals 11.
    Senggen Choi, May 17, 2012
    #3
  4. ravinder thakur

    Guest

    On Thursday, May 17, 2012 6:20:11 AM UTC+1, ravinder thakur wrote:

    > I am apparently puzzled by a very simple piece of code. What should be
    > the value of x after this statement ?
    >
    > int x = 10;
    > x = x++;
    >
    > I expect the value to be 10(since this is postfix). However the value
    > turns out to be 11. Any idea whats happening ?


    from the C FAQ
    http://c-faq.com/
    (and this is the same in C++)

    FAQ 3.1 "Why doesn't this code: a = i++; work?"
    , May 17, 2012
    #4
  5. Your example is funny since different compilers give different results:
    - g++ gives 11
    - clang gives 10

    However I can't tell you what the results shall be according to the
    standard (du to the assignement I would expect 11...)

    Sincerly,

    Helfer Thomas


    Le 17/05/2012 07:20, ravinder thakur a écrit :
    > hello guys,
    >
    > I am apparently puzzled by a very simple piece of code. What should be
    > the value of x after this statement ?
    >
    > int x = 10;
    > x = x++;
    >
    > I expect the value to be 10(since this is postfix). However the value
    > turns out to be 11. Any idea whats happening ?
    >
    > Thanks
    > xoxo
    Helfer Thomas, May 17, 2012
    #5
  6. Helfer Thomas <> wrote:
    > Your example is funny since different compilers give different results:
    > - g++ gives 11
    > - clang gives 10


    Both are correct. (In fact, if they produced -7 and 50391, they would
    have still been correct.)

    > However I can't tell you what the results shall be according to the
    > standard (du to the assignement I would expect 11...)


    It's undefined behavior, so anything goes.
    Juha Nieminen, May 17, 2012
    #6
  7. ravinder thakur

    Jorgen Grahn Guest

    On Thu, 2012-05-17, Helfer Thomas wrote:
    > Le 17/05/2012 07:20, ravinder thakur a écrit :
    >> hello guys,
    >>
    >> I am apparently puzzled by a very simple piece of code. What should be
    >> the value of x after this statement ?
    >>
    >> int x = 10;
    >> x = x++;
    >>
    >> I expect the value to be 10(since this is postfix). However the value
    >> turns out to be 11. Any idea whats happening ?


    > Your example is funny since different compilers give different results:
    > - g++ gives 11
    > - clang gives 10
    >
    > However I can't tell you what the results shall be according to the
    > standard (du to the assignement I would expect 11...)


    gcc gives a warning "operation on 'x' may be undefined". That should
    be a quite clear indication about what result you can expect, right?

    Never run your compiler without enabling full warnings.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, May 17, 2012
    #7
    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.

Share This Page