Problem with precedence in C

Reply

Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Problem with precedence in C

 
0
  #1
Dec 15th, 2005
I am having this problem with precedence of operators. Its with the increment and decrement operators.
  1. int i,j=1;
  2. i=(j++)+(++j)+(j++);
this evaluates to 6 instead of 7 . Why does this happen. It would be of great help to me.
Thanks,
comwizz
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem with precedence in C

 
0
  #2
Dec 15th, 2005
j++

means j == j +1

but ++j means add one before u do the next thing i thinks.

God bless.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Problem with precedence in C

 
0
  #3
Dec 15th, 2005
Originally Posted by iamthwee

j++ means j == j +1
I think you ment j = j + 1 right
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Problem with precedence in C

 
0
  #4
Dec 15th, 2005
Originally Posted by comwizz
I am having this problem with precedence of operators. Its with the increment and decrement operators.
  1. int i,j=1;
  2. i=(j++)+(++j)+(j++);
this evaluates to 6 instead of 7 . Why does this happen. It would be of great help to me.
Thanks,
comwizz
It looks like you're expecting the expression (j++)+(++j)+(j++) to be evaluated from left to right. The C standard does not require this. Because you are modifying your variable j more than once in the same expression, the behavior is undefined.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: Problem with precedence in C

 
0
  #5
Dec 17th, 2005
Originally Posted by Rashakil Fol
It looks like you're expecting the expression (j++)+(++j)+(j++) to be evaluated from left to right. The C standard does not require this. Because you are modifying your variable j more than once in the same expression, the behavior is undefined.
Even if we evaluate from the right for j=1 the expression will be evaluated like 1 + 3 + 3 and will still turn out to be 7. Do you mean that theres no conventional way of how to evaluate this expression?? If there is a way please do tell me.
Thanks,
comwizz.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Problem with precedence in C

 
0
  #6
Dec 17th, 2005
Originally Posted by comwizz
Even if we evaluate from the right for j=1 the expression will be evaluated like 1 + 3 + 3 and will still turn out to be 7.
You have no guarantee of when the increments happen. They could happen in the middle of the expression (as when you evaluate left-to-right or something), or they could happen after, or whenever.

(k = j++ + j++) might be treated equally as either k = j + j; j += 2; or k = j; j += 1; k += j; j += 1;

You need to separate your expression into multiple statements. For another example: Is y[i] = ++i; equivalent to y[i] = i; i += 1; or y[i + 1] = i; i += 1;?
All my posts may be redistributed under the GNU Free Documentation License.
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