Plz Solve this problem...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 5
Reputation: gaurav252 is an unknown quantity at this point 
Solved Threads: 0
gaurav252 gaurav252 is offline Offline
Newbie Poster

Plz Solve this problem...

 
0
  #1
Aug 20th, 2007
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;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Plz Solve this problem...

 
0
  #2
Aug 20th, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 5
Reputation: gaurav252 is an unknown quantity at this point 
Solved Threads: 0
gaurav252 gaurav252 is offline Offline
Newbie Poster

Re: Plz Solve this problem...

 
0
  #3
Aug 20th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Plz Solve this problem...

 
1
  #4
Aug 20th, 2007
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!)
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Plz Solve this problem...

 
0
  #5
Aug 20th, 2007
Read about Sequence Points
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Plz Solve this problem...

 
0
  #6
Aug 20th, 2007
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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Plz Solve this problem...

 
0
  #7
Aug 20th, 2007
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
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Plz Solve this problem...

 
0
  #8
Aug 20th, 2007
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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Plz Solve this problem...

 
0
  #9
Aug 20th, 2007
Originally Posted by hollystyles View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC