evaluate this

A

ashim.seth

hey group i was just testin this :
int i;
i=i++ + ++i;
cout<<i;
and then


int i= i++ + ++i;
cout<<i;

both givin different results
y?
plz help;
 
D

David Lago

My guess is that it happens because you're playing with uninitialized
memory. Which value is i expected to have when you do i++ + ++i??

Anyway... isn't this a conp.lang.c++ question?

David.

hey group i was just testin this :
int i;
i=i++ + ++i;
cout<<i;
and then


int i= i++ + ++i;
cout<<i;

both givin different results
y?
plz help;


--
David Lago <[email protected]>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
 
D

Don

Both of these invoke undefined behavior. A compliant compiler could
legitimately cause monkeys to fly out of your nose. First of all, you
use an uninitialized variable, and secondly, you alter a variable twice
between sequence points.

Don
 
D

David Lago

David said:
My guess is that it happens because you're playing with uninitialized
memory. Which value is i expected to have when you do i++ + ++i??

Well, you could say "whichever value it has, it should be the same in
both cases", but the behaviour in such cases is unspecified, so they
both could be different.

Greets,
David.
--
David Lago <[email protected]>

PGP key available at: http://pgp.mit.edu
Personal Blizog: http://arcanelinux.org/blog/dlworld
The Enflame Project: http://enflame.org
 
A

Artie Gold

hey group i was just testin this :
int i;
i=i++ + ++i;
cout<<i;
and then


int i= i++ + ++i;
cout<<i;

both givin different results
y?
plz help;

1) Please read the FAQ.
2) `cout' is not C. This is If you want C++, it's down
the hall.
3) Find your `caps' key.
4) Take the time and spell out your words.

--ag
 
C

CBFalconer

hey group i was just testin this :
int i;
i=i++ + ++i;
cout<<i;
and then

int i= i++ + ++i;
cout<<i;

both givin different results

Don't use silly abbreviations. Your code will not compile. It
never declares main, has undefined behaviour constructs, never
declares cout, left shifts cout by an uninitialized value and
discards the result (in two places). To use the word 'and' you
need to #include <iso646.h>. 'then' is also undefined. The first
person pronoun I is always in upper case. You can't have
executable code outside a function.

However you have managed to cram an unusual number of mistakes into
a few lines.
 
J

Jack Klein

Well, you could say "whichever value it has, it should be the same in

It doesn't have any value, it produces undefined behavior.
both cases", but the behaviour in such cases is unspecified, so they

There is no unspecified behavior present in the original code. The
term "unspecified behavior" has a precise definition in the C
standard, and nothing here exhibits unspecified behavior.
both could be different.

And then, of course, regardless of the validity of the object, the
code modifies it twice without an intervening sequence point,
producing still more undefined behavior.

It makes no difference what you, the OP, or anyone else expects.
There is no correct value. Or incorrect value.
 
A

Alan Balmer

hey group i was just testin this :
int i;
i=i++ + ++i;
cout<<i;
and then


int i= i++ + ++i;
cout<<i;

both givin different results
y?
plz help;

It's not C, and it's not English. I think you're in the wrong
newsgroup.
 
K

Kenneth Brody

Don said:
Both of these invoke undefined behavior. A compliant compiler could
legitimately cause monkeys to fly out of your nose. First of all, you
use an uninitialized variable, and secondly, you alter a variable twice
between sequence points.

And thirdly, he's using C++ not C. ("cout<<i;")


--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

CBFalconer

Kenneth said:
And thirdly, he's using C++ not C. ("cout<<i;")

No, that's perfectly legitimate in C, provided he has declared cout
as an unsigned integral type. Somebody should tell him that the
blank shortage is over.
 
D

Dave Thompson

No, that's perfectly legitimate in C, provided he has declared cout

Legitimate but silly.
as an unsigned integral type. Somebody should tell him that the

Nits: An unsigned integral type not strictly narrower than (thus
promotable to) signed int and set to any (representable) value, or a
signed integral type not narrower than int and set to a value which
when shifted left i bits is still representable (does not overflow)
that type, or a narrow integral type (or in C99 _Bool) whose value
when shifted left does not overflow int; and in all cases the value of
i is set and is nonnegative and is less than the width of (= number of
value bits in) the left-hand type or int if narrower.
blank shortage is over.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top