944,193 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1097
  • C++ RSS
Aug 20th, 2007
0

Plz Solve this problem...

Expand Post »
Hi . i m new in c++ programming..plz help me in sorting out this problem.....
HOW THE VALUE OF "j" VARIES IN FOLLOWING PROBLEMS ? Plz Explain...

1. int i=10,j;
j= (i++) + (i++);
cout<<j;

2. int i=10,j;
j= (i++) + (++i);
cout<<j;

3. int i=10,j;
j= (++i) + (i++);
cout<<j;

4. int i=10,j;
j= (++i) + (++i);
cout<<j;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gaurav252 is offline Offline
5 posts
since Aug 2007
Aug 20th, 2007
0

Re: Plz Solve this problem...

I hate it when teachers teach undefined behavior. The value of j will depend on the compiler you are using because the language does not define the behavior of i.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Aug 20th, 2007
0

Re: Plz Solve this problem...

But turbo C..compiler shows the value oj j in ...
1. 20
2. 22
3. 22
4. 24
But i unable to understand why its showing these values...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gaurav252 is offline Offline
5 posts
since Aug 2007
Aug 20th, 2007
1

Re: Plz Solve this problem...

It's the order that the sums and increments are executed. In each expresion you listed there are different addition operations to be done and depending on what order you do them effects the result. i++/++i is shorthand for i = i + 1; If you place the ++ before the i (prefix) this is generally accepted as increment i *before* evaluating the expression, putting ++ after the i (postfix) means evaluate the expression first and increment i *afterwards*. Also placing things in parentheses generally means evaluate this part of the expression first, but I imagine they are only in this example for clarity (there's only so many +'s the human eye can take!)
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Aug 20th, 2007
0

Re: Plz Solve this problem...

Read about Sequence Points
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 20th, 2007
0

Re: Plz Solve this problem...

Quote ...
But turbo C..compiler shows the value oj j in ...
It doesn't matter what the compiler does. The expression is undefined so the compiler is free to do anything, like give you 0 as the result regardless of j's value, or throw a system exception, or evaluate the expression in one of the ways it could be done. That's the problem. The expression could be evaluated in more than one way and they're all equally possible. So instead of just picking one, C++ says that the whole thing is undefined and you can't change a variable more than once between sequence points.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 20th, 2007
0

Re: Plz Solve this problem...

1. j= (i++) + (i++);

Compiles to machine instructions in this order:

j = 10 + 10
i = 10 + 1
i = 11 + 1

2. and 3.

i = 10 + 1
J = 11 + 11
I = 11 + 1

4.
i = 10 + 1
i = 11 + 1
j = 12 + 12
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Aug 20th, 2007
0

Re: Plz Solve this problem...

But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.
Last edited by hollystyles; Aug 20th, 2007 at 10:33 am.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Aug 20th, 2007
0

Re: Plz Solve this problem...

But like everyone else points out it's kind of nonsense, because 1. why would you ever need to do that anyway? Obfuscation? other than that I can't think of anything. and 2. it's contrary to the standard.
and 3. he has an idot for a teacher.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: splaying algorithm
Next Thread in C++ Forum Timeline: How to convert very long string to a double???





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC