Can someone tell me how prefix / postfix operators really work? I’ve been looking online a lot but haven’t found anything.
From what I can tell prefex first increments, then does the operation and then assigns.
Postfix would do the operation first, then assign and then increment.
But I’m having a bit of trouble with my code:
int x, y;
x = 1;
y = x + x++; // (After operation y = 2)(x=2)
However when I do:
y = x++ + x; // (After operation y = 3)(x=2)
I’m not sure why these operations would be any different. I have two questions:
-
Could you explain the difference?
-
How does this apply to the other operator Prefix?
+are evaulated in left-to-right order.+is unspecifed.For C# your examples work as follows: