Someone Please explain this:
int a=3,b;
b=++a+(++a);
This gives b=10 when expected value is b=9?
10 1913
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
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
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
But point is that the other method wherein b=a+a always gives the answer
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
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
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.
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
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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 &)
|
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------------------------------...
|
by: Saturday7 |
last post by:
You have a struct:
struct a {
char *p;
};
struct a *apple;
Which does this preincrement: apple or p?
|
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,...
|
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...
|
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...
|
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,...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |