Difference between return & exit(0)
June 12, 2012
Search in a matrix sorted on rows & columns
June 12, 2012

value of i++ + ++i or ++i + i ++

If the initial value of i is 2 What is the value of expression i++ + i++?

Solution:

It’s undefined. Both in C & in C++.

The standard says:

“If you read a variable twice in an expression where you also write it, the result is undefined.”

So you should never do that 🙂

Another example is:

arr[i] = i++;

More undefined behavior: In the below example also, the values passed to the function are not defined irrespective to the value of i.

fun(arr[i], i++);

Here, the result is undefined because the order of evaluation of function arguments is undefined

Leave a Reply

Your email address will not be published. Required fields are marked *