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?

Dani AI

Generated

Short answer: the division error happens because integer division on the inner expression yields zero, so you end up dividing by 0. The second case (the expression that mixes a multiplication, a post-increment of the same variable, and a pre-decrement of the other variable, all into a single assignment) is not something the language guarantees a value for — different compilers/optimizations can produce different results. As showed, a plain algebraic substitution can give 19 and that’s what observed at runtime, but and are correct that the standard treats the combined modification as undefined. (cppreference.com)

Why: C++ separates “value computation” from “side effects” and requires that side effects on the same scalar be properly sequenced. A post-increment produces a value and separately schedules the increment side effect; the assignment schedules a write to the same object. If those side effects are not sequenced relative to each other, the behavior is undefined — the standard does not promise any particular outcome. Put another way: observing 19 on your machine is useful only diagnostically; it isn’t guaranteed by the language. (cppreference.com)

How to fix (well‑defined rewrite). Capture then apply in separate steps so every modification is sequenced:

int x = 10, y = 5;
int oldY = y;      // capture value to use (what y++ would have supplied)
y = oldY * 2;      // use captured value
x = x - 1;         // do the decrement explicitly (pre-decrement effect)
y = y + x;         // final, well-defined assignment

Always avoid modifying a variable more than once in a single expression; break the logic into clear statements or temporaries. That removes the ambiguity and makes the code portable and optimizable. (cppreference.com)

Recommended Answers

All 14 Replies

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?

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.

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.

Because y is modified twice. See this.

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?

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++; ?

commented: That was easy to understand... +3

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.