CS 1124 — Object Oriented Programming

Frequently Asked Questions

Q: How do I turn on line numbers in the .Net C++ editor?

A: Under the Tools menu:

Q: I have the following code:

int i = 0;
cout << ++i << ' ' << i << endl;

I expected the output to be

1 1

but intead it was

1 0


A: There is actually no way of telling which output you should see.

C++ does not guarantee what the "order of evaluation" will be of terms in an expression. A C++ compiler is free to evaluate the second use of i first and only then go and evaluate ++i. That would result in the output that you saw. A different C++ compiler might do it the way you expected. The lesson is that you should not depend on the order in which things get evaluated. Instead write the above code as:

int i = 0;
cout << ++i << ' ';
cout << i << endl;



Maintained by John Sterling (jsterling@poly.edu). Last updated January 23, 2011