# Thread: Evaluation of arithmetic expression that involves increment operators

1. ## Evaluation of arithmetic expression that involves increment operators

Here is a simple code to evaulate an arithmetic expression :

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);

}```
As already said when this code is executed the value of a is printed as 50.

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?

2. 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++;`
that is terrible code. does it mean?
Code:
```x=y;
y++;```
or
Code:
```y++;
x=y;```
the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

(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)

3. 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 ?:.

4. Originally Posted by abc1000

As already said when this code is executed the value of a is printed as 50.

The ONLY answer, is, the result is undefined. Period. End of story.
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.

5. You should never post/pre increment or decrement a variable within the same code statement. This will surely lead to undefined behavior.

6. Originally Posted by Brain_Child
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++;`
that is terrible code. does it mean?
Code:
```x=y;
y++;```
or
Code:
```y++;
x=y;```
the answer? who cares. we could compile and run it and find out but that isnt the problem. if anyone should ever ask that question about your code, then the code is ambigious and should be fixed so that it is absolutely clear what the program isa meant to be doing.

(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)
You're wrong. The OP code is wrong, but your example is not. The code:
Code:
`x=y++;`
is perfectly fine and it means:
Code:
```x = y;
++y;```
There's no uncertainty or implementation defined behaviour about it.

7. Originally Posted by MisterIO
You're wrong. The OP code is wrong, but your example is not. The code:
Code:
`x=y++;`
is perfectly fine and it means:
Code:
```x = y;
++y;```
There's no uncertainty or implementation defined behaviour about it.
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.