Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Written Test
The real background of the story is about sequence points.
Increment operators are defined that the side-effect can happen anytime between the two sequence points where it appears. So j++ can happen anytime of the two ";"s that are "around" the assignment in question (; i = j++ + j++;) - the ;s define the sequence points - for more info check the C standard)
And it is sure that the two increments are applied after the ";" - the closing sequence point of the assignment statement.
void main() {
int i,j;
j = 10;
i = j++ - j++;
printf("++j - j++%d %d \n\n", i,j);
j = 10;
i = ++j - ++j;
printf("++j - j++%d %d \n\n", i,j);
j = 10;
i = j++ - ++j;
printf("++j - j++%d %d \n\n", i,j);
j = 10;
i = ++j - j++;
printf("++j - j++%d %d \n\n", i,j);
getch();
}
You get the same answer for all the above code because the expression is first evaluated and the (pre/post)-incremental takes place...
Just experiment with code :-)
Here is my example how to see what is going on.
template <typename T>
class op_test
{
private:
T val_;
op_test();
public:
explicit op_test(const T& v)
: val_(v)
{
}
virtual ~op_test(){}
op_test& operator-(const op_test& rhs){
std::cout << "called operator-" << std::endl;
val_ = val_ - rhs.val_;
return *this;
}
op_test& operator++(int i)
{
std::cout << "called operator++" << std::endl;
val_ += 1;
return *this;
}
};
int main()
{
op_test<int> a(12), res(0);
res = a++ - a++;
}
It produce following result for G++
called operator++
called operator++
called operator-
I think if you overload the operator, it is more like a function call rather than post operation. This is why it is showing in the order you printed
I think if you overload the operator, it is more like a function call rather than post operation. This is why it is showing in the order you printed
j++ j value is incremented after the execution of statement
++j j value is incremented while the statement is executed
i=j++ - j++;
so above indicates i=10-10
however if
i=++j - ++j;
i value can be compiler dependent
so i value is 0 and j value is 12 as it is incremented twice after the execution of statement
i=j++ - j++;
That's trivial ....
we have i = 10 -10 = 0 //post increment of j, means we use 10 rather than 11 ,then we increment that
then we see that j increment 2times which gives it the value of 12
It is not correct.
According en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B substract has less priority then increment.
So it seems to me 0 because of 12 - 12
The result is undefined. Side effects in an expression do not have a set order of evaluation in C. The outcome is up to the compiler's implementation.
- C++ Lion October 06, 2011A compiler may decide to evaluate the side effects of an expression at a different time. The side effect of j++ is permanently incrementing the value of j after using the current value.
This is not a simple expression. This is an arithmetic expression containing two side effects. One of the side effects can potentially impact the rest of the processing.
Since the subtraction has a set operator order of precedence (left-to-right), the code will first compute (j++). The value of which is the current value of j, 10. This is the left-hand side of the subtraction, the minuend. The side effect making j permanently represent a value one higher (j+=1) may not occur until later (the end of the line's execution), even though this evaluates.
Next, the right-hand side of the subtraction, the subtrahend, will evaluate. Now comes the reason we have an undefined outcome. Did the side effect of evaluating the minuend take effect immediately, or will it not take effect until the line is completed? The compiler decides this question, then evaluates (j++) again, yielding a value of 10 or 11.
With ++j - ++j, you may find the same issue. Do we get 11-12 (side effect happens immediately) or 11-11 (side effect happens at the end of the line's execution)?
Do not depend on side effects happening at a particular time in code.