I am not satified with the previous answers. First of all, the PRECEDENCE (not priority) of the ++ operator is higher than the assignment. So, in z = y++; y is loaded with a value of 10, then y is incremented, then the expression is assigned to z, which is why z is 10. The increment is performed after the expression is evaluated. In effect, what happens is this: temp = y; y = y + 1; z = temp; In the case of x = x++, you have what is called an ambiguous operation, and the standard states that "the behavior is undefined". You will find different results in different compilers. One compiler may do the assignment before the increment, and another may do the assignment after the increment: Case 1: temp = x++; (assign 10 to temp (expression evaluated)). x = temp; increment x; result is x == 11. Case 2: temp = x++; (assign 10 to temp (expression evaluated)). increment x; assign temp to x; result is x == 10. On 6 Jul 2002 at 6:31, Verdi March wrote:
Hi, I'd like to ask a (basic) question about operator post inc (lvalue++). I've this code fragment:
/**************/ int y = 10; int z; z = y++; cout << "z = " << z << '\n';
int x = 10; x = x++; cout << "x = " << x << '\n'; /**************/
The execution results in: z = 10 x = 11
I'm confused why x became 11, because the post inc. operator has higher precedence than assignment, so given an expressoin x = x++, x++ is evaluated first. The signature for post inc. is T operator++(int). So the return value of x++ is 10 (x itself will be modified to 11), but then the expression of x = (x++) become x = ret_val_of_x_plus_plus, which is x = 10? Am I correct?
Regards, Verdi
-- GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists/archive/suse-programming-e
-- Jerry Feldman Enterprise Systems Group Hewlett-Packard Company 200 Forest Street MRO1-3/F1 Marlboro, Ma. 01752 508-467-4315 http://www.testdrive.compaq.com/linux/