Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
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?
 
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
 
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?

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.
 
Last edited:
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.

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:
testinc.c: In function ‘main’:
testinc.c:6: warning: operation on ‘i’ may be undefined

Upon execution:
i is: 1 x is 2

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
 
Question: How many possible values are there for the expression (i++) + (i--)?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.