473,325 Members | 2,342 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,325 software developers and data experts.

Preincrement operator

Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?
May 10 '07 #1
10 1913
pradeep kaltari
102 Expert 100+
Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?
Hi,
In the statement b=++a+(++a), "a" is incremented twice before the execution of this statement. As a result of this "a" contains value 5 in it.

Now the operation that has to be performed is b=a+a i.e, b=5+5. Hence the result.

Regards,
Pradeep
May 10 '07 #2
JosAH
11,448 Expert 8TB
Someone Please explain this:
int a=3,b;
b=++a+(++a);

This gives b=10 when expected value is b=9?
The result is undefined. The reason is that an lvalue may not be modified more
than once before a sequence point has been reached. If this 'rule' isn't obeyed
undefined behaviour is the result.

kind regards,

Jos
May 10 '07 #3
AdrianH
1,251 Expert 1GB
The result is undefined. The reason is that an lvalue may not be modified more
than once before a sequence point has been reached. If this 'rule' isn't obeyed
undefined behaviour is the result.

kind regards,

Jos
Jos is right,


Adrian
May 10 '07 #4
But point is that the other method wherein b=a+a always gives the answer
May 10 '07 #5
AdrianH
1,251 Expert 1GB
But point is that the other method wherein b=a+a always gives the answer
Sorry, the point is it is not defined. What you get may vary from compiler to compiler. It is not portable and its result is not predictable because there is no definition as to what it means.

So stop trying to be cool by making obfusticated code and deal. ;)


Adrian
May 10 '07 #6
AdrianH
1,251 Expert 1GB
But point is that the other method wherein b=a+a always gives the answer
Also, b=a+a is valid. So is b=++a+a and b=a+(++a), it is just b=++a+(++a) that is not, because of the reason already explained.


Adrian
May 10 '07 #7
Banfa
9,065 Expert Mod 8TB
So is b=++a+a and b=a+(++a)
Hi Adrian, I do not believe that these 2 examples are valid, I think they still invoke undefined behaviour due to this clause in the standard
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored."
In these 2 statements the prior value is not accessed only to determine the value to be stored.

This is discussed more fully here.
May 12 '07 #8
AdrianH
1,251 Expert 1GB
Hi Adrian, I do not believe that these 2 examples are valid, I think they still invoke undefined behaviour due to this clause in the standard
"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored."
In these 2 statements the prior value is not accessed only to determine the value to be stored.

This is discussed more fully here.
Yeah, I think you're right. I'll look into it more later.

Thanks,


Adrian
May 12 '07 #9
The result is undefined. The reason is that an lvalue may not be modified more
than once before a sequence point has been reached. If this 'rule' isn't obeyed
undefined behaviour is the result.

kind regards,

Jos
can you explain this more......
and also tell this
int i=5,y=0;
y=(++i)+(i++)+(i++)+(++i);
printf("i=%d\ny=%d",i,y);

now microsoft compiler gives out put
i=9
y=28
and gcc compiler gives out put
i=9
y=25

so tell which one is correct in terms of standardization
and logically which one is best
if you have any links please provide that too to support your answer
also tell which is the best compiler for c
Jun 15 '07 #10
sicarie
4,677 Expert Mod 4TB
pslcoder-

You have already started a thread on this, it is rude to hijack a thread (posting your question in someone else's question). Allow the people on this site time to respond before replying to your own thread that you started.

Please read this if you have any further questions.
Jun 15 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
14
by: Jinesh | last post by:
Dear Guru's: Have a simple C questions. Is this a bug or am I doing something reallllly stupid? Here's the code snippet:- ---------------------------Code------------------------------...
7
by: Saturday7 | last post by:
You have a struct: struct a { char *p; }; struct a *apple; Which does this preincrement: apple or p?
7
by: Huck Phin | last post by:
OK, so I have looked and looked for something similar to my problem, and I cannot find one. I am writing a program for a HugeInteger, and I am attempting to overload the preincrement,...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
6
by: vlsidesign | last post by:
If I have a "for" loop like this: for (i = 0; i < 10; i++) or like this for (i = 0; i < 10; ++i) both of the "for" loops will function the same. I also noticed if I use a variable called j...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.