Let's have an array a = {0,1,2,3,4}
and an int i = 2
. Now let's do few operations in both (always starting from the point above).
a[i] = i++; // a = {0, 1, 2, 3, 4}
a[i] = ++i, // a = {0, 1, 3, 3, 4}
This seems logical to me. But for C++, I get different results:
a[i] = i++; // a = {0, 1, 2, 2, 4}
a[i] = ++i; // a = {0, 1, 2, 3, 4}
I don't understand the results I get in C++;
C# evaluates from left to right, so in the first case, you get:
a[2] = 2; // 1)
a[2] = 3; // 2)
In C++, that is undefined behavior, but since C++17, an assignment operator is evaluated from right to left:
a[3] = 2; // 1)
a[3] = 3; // 2)
Different languages, different rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With