![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 1
| Evaluation of arithmetic expression that involves increment operators a=(++a)+(++a)+(a++)+(a++) where a is an integer . Taking an initial value of a=10,we have, a= (++10)+(++a)+(a++)+(a++) = 11+(++11)+(a++)+(a++) = 11+12 +(12++)+(a++) = 11+12+12+(13++) = 11+12+12+13 a= 48 A simple Code executed using Turbo C++3.0 on Windows OS gives an output of 50 for the same. CODE: Code: #include<stdio.h>
void main()
{
int a=10;
a=(++a)+(++a)+(a++)+(a++);
printf("%d",a);
}
Thus i am most certain that i have gone wrong in evaluating the expression myself as i have got 48 as the answer. Can anybody identify this error and tell me how this expression gets evaulated? Thanks in advance. |
| abc1000 is offline | |
| | #2 |
| Registered User Join Date: Feb 2009
Posts: 35
| you shouldnt try to work out this problem because your code is just poor and you should never have anything at all resembling that line in a real program. why? for the very reason that you are confused about this problem. you should only do one thing at a time. it makes your code clear. eg Code: x=y++; Code: x=y; y++; Code: y++; x=y; (btw, if you are dying to know, your arithmetic is wrong. go re read what ++a and a++ do. its a little different to what you thikn they do, which is clear after looking at your arithmetic) |
| Brain_Child is offline | |
| | #3 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| Expressions are not necessarily evaluated left to right. So when you substitue the value of a in your evaulation, you and the implementation has a choice of when to do so. It might sub in 10 first for all the a's for example, or it might go in the order you chose, or something else. Additionally, a++ and ++a do three things -- they fetch a, they compute a+1, and they change the value of a. There is no guarantee that all three things happen one right after another; the compiler can mix in other operations between them. This is a problem one of the operations mixed in is assignment to a, because that assignment may simply be lost when the program assigned the result of a+1 (computed based on the original a) to a. C is made for optimizations like this. The problems here are resolved by stating that you cannot assign or change the value of a memory location more than once between two sequence points. Nor can you retrieve and assign to a memory location between two sequence points except when the retrieved value is used to compute the new value. Seqence points are either the end of an expression (usually marked by a ;, or one of the sequencing operators &&, ||, ,(comma), and ?:.
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. |
| King Mir is offline | |
| | #4 | |
| Registered User Join Date: Aug 2006
Posts: 76
| Quote:
50 is not right. 48 is not right. 32.8765 is not right. It is undefined. Trying to figure out an actual number is a 100% waste of time. | |
| rdrast is offline | |
| | #5 |
| Registered User Join Date: Jan 2008 Location: Seattle
Posts: 476
| You should never post/pre increment or decrement a variable within the same code statement. This will surely lead to undefined behavior. |
| slingerland3g is offline | |
| | #6 | |
| Registered User Join Date: Nov 2008
Posts: 75
| Quote:
Code: x=y++; Code: x = y; ++y; | |
| MisterIO is offline | |
| | #7 |
| Ex scientia vera Join Date: Sep 2007
Posts: 426
| This guy is right. What brain_child said is complete bull......... There would be no point in having the post/pre incremement operators if they couldn't be used that way. And there is a reason you have both post and pre increment operators.
__________________ "What's up, Doc?" "'Up' is a relative concept. It has no intrinsic value." |
| IceDane is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Screwy Linker Error - VC2005 | Tonto | C++ Programming | 5 | 06-19-2007 02:39 PM |
| recursion error | cchallenged | C Programming | 2 | 12-18-2006 09:15 AM |
| Increment / Decrement Operators - Help | shyam168 | C Programming | 6 | 03-29-2006 09:24 PM |
| Question on l-values. | Hulag | C++ Programming | 6 | 10-13-2005 04:33 PM |
| Please Help - Problem with Compilers | toonlover | C++ Programming | 5 | 07-23-2005 10:03 AM |