# Output for ++i*++i*++i ?

This is a discussion on Output for ++i*++i*++i ? within the C++ Programming forums, part of the General Programming Boards category; Hi, This question is from my exam: Predict the output of the following program: Code: #include <iostream> using namespace std; ...

1. ## Output for ++i*++i*++i ?

Hi,

This question is from my exam:

Predict the output of the following program:
Code:
```#include <iostream>
using namespace std;
int main()
{
int i=3,j;
j=++i*++i*++i;
cout<<j;
return 0;
}```
I solved it like this:

4*5*6= 120
But the correct answer (according to the teacher) is:

6*6*6 = 216
When I compile this program, it gives warning: operation on ‘i’ may be undefined

and then prints out 150 as the output!

Thank you.

2. The teacher is right. 216 is a correct answer. You are also right. 120 is a correct answer. The compiler is right. 150 is a correct answer.

The reason is that the behaviour is undefined. Read Stroustrup's answer to the FAQ: What's the value of i++ + i++?

EDIT:
In case you were interested, MSVC8 (Microsoft Visual C++ 2005) gives the answer as 216, the MinGW port of g++ 3.4.5 gives the answer as 150.

3. Originally Posted by laserlight
The teacher is right. 216 is a correct answer. You are also right. 120 is a correct answer.
I will state it another way - You and teacher are wrong.
The compiler is right - behavior is undefined.

And as a result correct answer will be:
Result of the progam is unpredictable. -1 is also a reasonable good result for operation involsing undefined behavior...

4. I will state it another way - You and teacher are wrong.
The compiler is right - behavior is undefined.
They can write their own compilers to produce the answers they like

5. Unfortunately your teacher has neglected to teach you about sequence points.
Originally Posted by Wikipedia
A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.

Assignment is an operation that has side effects. This was mostly explained to me by Gerald J. Sussman when he talked about it during his lecture videos based on SICP.

The idea being that i initially has a value, and that value changes when you assign it a new one (of course). This is a problem because the change in value of i has effected the correctness of any result produced by previously exzecuted code depending on it's value.

As the article states, it's come up for you in this code, because due to a lack of sequence points, the compiler is free to evaluate your math equation in several ways, and be "right."

Fix i's value before you cube it.

6. Originally Posted by laserlight
They can write their own compilers to produce the answers they like
That what comes to my mind. I like -1 answer. I think it should be returned for any operation relying on the undefined behavior for consistency.

7. Thanks for all the answer.

But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"

8. Just as showing numbers from 1 to 99 that all are less than 100 to prove that all natural numbers are less than 100

9. Originally Posted by abk

But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"
Not only does your teacher preach undefined behavior, but uses Turbo C, as well...
I feel sorry for you :/

10. Originally Posted by Elysia
Not only does your teacher preach undefined behavior, but uses Turbo C, as well...
I feel sorry for you :/
No need to feel sorry. I'm a member of cboard, where I get my problems solved. I just get some good sleep in his lectures

11. Then I feel sorry for your classmates.

12. But I won't argue with my teacher on that one, coz if I do, he will simply execute that thing in Turbo C++ and show me the result. "216"
One tactic would be to ask your teacher for help in explaining the output of this program:
Code:
```#include <iostream>
using namespace std;
int main()
{
int i=3,j;
j=i++ + i++;
cout<<j;
return 0;
}```
Ensure you have a computer with Internet access handy. When your teacher is nearly done with the explanation, say that you got this from Stroustrup's homepage, and then present that FAQ that I linked to. If your teacher happens not to know who Stroustrup is, click on the "bio" link. The claim that 'Bjarne Stroustrup is the designer and original implementer of C++ and the author of "The C++ Programming Language"' can easily be verified by borrowing a copy of that book from your local library. Plus, if you have a copy of the C++ Standard, you might find something like this in the acknowledgements section: "The C++ programming language as described in this International Standard is based on the language as described in Chapter R (Reference Manual) of Stroustrup: The C++ Programming Language (second edition, Addison-Wesley Publishing Company, ISBN 0–201–53992–6, copyright &#169; 1991 AT&T)."

Of course, you probably have little to gain from this in terms of marks: if I was your teacher, I would just void the question... so those who guessed the answer that matched the model answer would get a little upset with you

13. Originally Posted by laserlight
Of course, you probably have little to gain from this in terms of marks: if I was your teacher, I would just void the question... so those who guessed the answer that matched the model answer would get a little upset with you
Or give everyone who answered a numeric answer "half score", and full points for "undefined behaviour".

--
Mats