# Possible values for an expresson...

Discussion in 'Mac Programming' started by cybrscot, Jan 10, 2011.

1. ### cybrscot macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
#1
I would like it if someone can check my answer on this...

Question: How many possible values are there for the expression (i++) + (i--)? What are the values, assuming that i has the value 1 initially?

So, i =1

I know that the postfix operators can be tricky because we don't know if the compiler will perform the operation immediately, or later.

I think that there are 3 possible values for this expression

first, can be calculated with both sets of post fix operators performed at the same time
second, can be calculated with the first set operator performed, while the second set is not.
third, can be calculated with the first set not being performed yet, while the second set is.

I don't think that there is a solution in which neither of the sets of post fix operators are performed because the compiler must perform before moving to the next line.

Thus, I think the possible answers are 2, 3, 1
i=1

(i++) + (i--) also (2) + (0) =2
(i++) + (i--) also (2) + (1) = 3
(i++) + (i--) also (1) + (0) = 1

Am I correct?

2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
I'm afraid not. The postfix operators don't happen "sometime", that wouldn't really be helpful. They happen AFTER the expression has been evaluated with the original value. So the expression in question is always going to evaluate to i + i.

What is i going to be AFTER the expression is evaluated? Well, that seems like it might be worse, but is it really? Does it matter if the compiler increments first or decrements first? Even if you overflow, you're going to immediately underflow, or vice versa.

I guess this could get tricky if it were:
i = (i++) + (i--);
but that's not what you posted. Even if this were the case, it's not "unclear", it just seems a little trickier.

-Lee

3. ### lloyddean macrumors 6502a

Joined:
May 10, 2009
Location:
Des Moines, WA
4. ### cybrscot thread starter macrumors 6502

Joined:
Dec 7, 2010
Location:
Somewhere in Southeast Asia
5. Jan 10, 2011
Last edited: Jan 10, 2011

### gnasher729 macrumors P6

Joined:
Nov 25, 2005
#5
Assuming C or C++. If you are talking about Java, your answer is completely wrong in a totally different way.

Google for N1256.pdf, the latest free draft of the C Standard.

6.5.2: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored."

(i++) + (i--) modifies the object i twice between the previous and the next sequence point. This results in undefined behaviour. This in turn means that anything at all is possible, including your program crashing right then, or your program crashing and destroying any connected hard drive when you give the program to a customer.

You have left safe ground of the C language. You are doing things where the C Standard (and likewise the C++ Standard) says that they are not allowed, and that whatever happens is your fault.

6. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#6
Oops. I had never done this because it's nonsense, I was just working on the basis of when a post-increment happens. I don't know if the book was trying to be tricky or not. In any event...
Code:
```#include <stdio.h>

int main(int argc, char *argv[]) {
int i = 1;
int x;
x = (i++) + (i--);
printf ("i is: %d\tx is %d\n",i,x);
return 0;
}
```
compiled with gcc -Wall produces:
Upon execution:
So it looks like GCC will tell you you're doing something terrible, but handles it in the way one might hope for (in this case, for this execution of the code, at least).

-Lee

7. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Brobdingnag
#7
http://en.wikipedia.org/wiki/Sequence_point

For expressions like this, the compiled optimization level may also affect the result. This would be due to code reordering between sequence points that's only done above some optimization level, but not done for unoptimized or less-optimized code.

8. ### dmi macrumors regular

Joined:
Dec 21, 2010
#8
The expression has an infinite number of possible values.
The C standard requires that "Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression."
A violation of this requirement means that the behavior is undefined.