473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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
Jul 22 '05 #1
10 2049
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
Jul 22 '05 #2
PKH

"Andreas Sheriff" <sp*********@iion.com> 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

Jul 22 '05 #3
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.

Jul 22 '05 #4
On Fri, 24 Sep 2004 00:53:55 -0700, "Andreas Sheriff"
<sp*********@iion.com> 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
Jul 22 '05 #5
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 :)

Jul 22 '05 #6
nebjy <th******************@hotmail.com> 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
Jul 22 '05 #7
Simon Stienen wrote:
nebjy <th******************@hotmail.com> 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.
Jul 22 '05 #8
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
Jul 22 '05 #9
nebjy <th******************@hotmail.com> wrote:
Simon Stienen wrote:
nebjy <th******************@hotmail.com> 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
Jul 22 '05 #10
On Fri, 24 Sep 2004 11:48:51 +0200, "PKH" <no************@online.no>
wrote in comp.lang.c++:

"Andreas Sheriff" <sp*********@iion.com> 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/~a...FAQ-acllc.html
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlos Ribeiro | last post by:
I was checking the Prolog recipe in the Cookbook: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303057 It's a clever implementation that explores some aspects of Python that I wasn't...
1
by: MSDousti | last post by:
Hi, I'm confused with C++'s unary increment operator(++). Consider the following code: ////////////////////////////////////////////////////////////////////// #include <iostream> using...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
4
by: Alistair Welchman | last post by:
I have a Hashtable of ints keyed on Guids, and I want to do the following: foreach ( DataRow row in dsWorkDays.Tables.Rows ) { Guid PersonId = (Guid)row; DateTime Day = (DateTime)row;
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
11
by: divya_rathore_ | last post by:
The code: int aaa = 100; printf("%d %d %d\n", --aaa, aaa, aaa--); printf("%d\n", aaa); prints: 99 100 100 98
13
by: jehugaleahsa | last post by:
Hello: In C++, you had to distinguish between post and pre increments when overloading. Could someone give me a short demonstration of how to write these? I get the impression that are...
16
by: JoseMariaSola | last post by:
How may operators and operands does (typename) expression has? I'd say one operator, the cast operator, and two operands: typename and expression. But everywhere I read, cast is categorized as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.