I was studying for an exam and came across two questions that me and my friends didn't know the answer to. We are given the answer but we just can't figure out what the answer is what it is.

First, why does this code give an error?

int z=5, q=2;
cout << 10/(q/z) << endl;

Second, what does this code equal? I believe it equals 19 but our teacher says it is 20.

//Given x=10 and y=5

y = 2*y++ + --x;
cout << y << endl;

Wouldn't y = 2*y++ + --x just be the same as 2*5 + 9 because y++ is post-processing which means that after this line is run y is now equal to 6 and not 5? And --x is equal to 9 because it is pre-processing so 10 would be decremented to 9 before the math?

Edited 4 Years Ago by pattmorter: n/a

I figured out why cout << 10/(q/z) << endl; gives an error. integer division and division by 0. but i still dont understand the second question.

Did you put the second example in a small program and run it?

yeah the output is 19

>>//Given x=10 and y=5

y = 2*y++ + --x;

y = (2*y++) + (--x)

//substitute, not valid but easier to see
y = (2 * 5++) + (--10);
y = (2 * 5) + (--10) //5++ returns 5 first then increments, so 5 will be multiplied to 2
y = 10 + (--10)
y = 10 + 9 //--10 decrements first then returns so --10 decrements to 9 first
y = 19

I wonder if some compilers would actually increment the y during multiplication.
...that seems to violate a math rule, though.

yeah that is why i figured id ask here. i personally think she just made a typo. but i wanted to make sure.

Second, what does this code equal? I believe it equals 19 but our teacher says it is 20.

Run away from that teacher as fast as you can.

y = 2*y++ + --x;

modifies y twice (by means of postincrement, and by means of assignment) with no sequence point in between. Undefined behaviour, end of story. No answer is correct.

Edited 4 Years Ago by nezachem: n/a

Run away from that teacher as fast as you can.

y = 2*y++ + --x;

Correct me if I'm wrong. But how is this an undefined behavior? I agree that the below line will give undefined behavior.

x = y++ * --y;

Because we cannot predict which one (y++ or --y) will be evaluated first.
But In this case

y = 2*y++ + --x;

2*y++ and --x are independent. In whatever order they are executed, we get the same result. And only after the result is got, it will be assigned to y. I may be wrong here, but I would like to know what is the undefined behavior here.

ok. Thanks. So

i = i++;

is undefined because of two assignments happening
1. modification done using = operator.
2. modification done by ++ operator.

So if my understanding is correct this assignment should be okay and doesn't have undefined behavior.

i = ++i;

Am I right?

Am I right?

No. The y++ differs from ++y in the value calculated by the operator. A side effect still happens at an unspecified moment somewhere prior to a sequence point. The behaviour is still undefined.

A side effect still happens at an unspecified moment somewhere prior to a sequence point.

I'm sorry to say I didn't understand this line. I also googled about undefined behavior and sequence point. I could find nowhere anybody mentioning about pre increment operator, though there are many posts which gives undefined behavior while using post increment. In the example that I gave,

i = 10;
i = ++i;
//step 1 : increment happens, i becomes 11.
//step 2 : i = 11; assignment is going to happen with 11.
//step 3 : value of i becomes 11 after assignment.

My question is, ++ operator already did its job in step 1. So what is the problem?

Edited 4 Years Ago by subith86: added more clarity on step2

Given i = ++i; Does ++ change i?
Does = change i?
If the answer is yes to both, undefined.

Why would it be different from i = i++; ?

Comments
That was easy to understand...

ok. Thanks. So

i = i++;

is undefined because of two assignments happening
1. modification done using = operator.
2. modification done by ++ operator.

So if my understanding is correct this assignment should be okay and doesn't have undefined behavior.

i = ++i;

Am I right?

How is it undefined? final value of i should always increase by 1

... please ignore i take it back :D

Edited 4 Years Ago by Ali_2101: n/a

This article has been dead for over six months. Start a new discussion instead.