Velocity Reviews > C++ > opertor ++

# opertor ++

ravinder thakur
Guest
Posts: n/a

 05-17-2012
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

red floyd
Guest
Posts: n/a

 05-17-2012
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.

Senggen Choi
Guest
Posts: n/a

 05-17-2012
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.

nick_keighley_nospam@hotmail.com
Guest
Posts: n/a

 05-17-2012
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] = i++; work?"

Helfer Thomas
Guest
Posts: n/a

 05-17-2012
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

Juha Nieminen
Guest
Posts: n/a

 05-17-2012
Helfer Thomas <(E-Mail Removed)> 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.

Jorgen Grahn
Guest
Posts: n/a

 05-17-2012
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 .

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules