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
Verdi March wrote:
int x = 10; x = x++; cout << "x = " << x << '\n';
The execution results in: x = 11
As I recall, the increment is done after the expression is evaluated in full - I can't actually think of any other sensible place to do it. So the compiled code should do something like move x to x # x = 10 and probably optimised out increment x # x = 11 This makes more sense if you think how you might generate assembly code from an expression. In a similar way int x = 10; x = y = x++; cout << "x = " << x << '\n'; cout << "y = " << x << '\n'; will give x = 11 y = 10 I would avoid this kind of code in a real program because it's inefficient, difficult to read, and there may be a danger that some compiler will get it wrong. On the other hand, it's a good way to find out how C++ works. JDL -- Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Hello, The postincrement operator does NOT have a higher priority than assignment. First, the assignment is done, then the increment is done. The preincrement operator has a higher presedence than assignment (++x). With preincrement, first the variable is incremented then the result is assigned. And your problem is not with operator presedence. Let me explain you what happens for x step by step: 1. x is created in the memory and the value 10 is assigned 2. x is assigned to x, in other words value of x is set to value of x (so x is 10) 3. The value stored in x is incremented by 1 (so now x is 11) 4. x is printed to the stdout (the printed value is 11) In your code it seems as if you are trying to print the old value of x (the value before the increment). For this to happen you should use z = x++; cout << "x = " << z << '\n'; instead of x = x++; cout << "x = " << x << '\n'; This way you will be able to print the old value. To make the things quite clear 1. x is created in the memory and the value 10 is assigned 2. x is assigned to z, in other words value of z is set to value of x (so z is 10) 3. The value stored in x is incremented by 1 (so now x is 11) 4. z is printed to the stdout (the printed value is 10). I hope my explanation contains the answer to your question. 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
-- Sedat Dogru __________________________________________________ blue tec IT services ag Sedat Dogru Project Manager Kleinhadernerstr. 40 80689 München Germany +49 - 89 - 700 76 43- 46 mail: sdogru@blue-tec.net internet: www.blue-tec.net
Hi, from the result (print x=11), post increment is evaluated after assignment (agree with you), meaning the post increment operator is not higher than assignment. But I decided to post to this list is because on my reference book, post increment has higher precedence. Actually this is the source of my confusion... Regards, Verdi
Hello, The postincrement operator does NOT have a higher priority than assignment. First, the assignment is done, then the increment is done. The preincrement operator has a higher presedence than assignment (++x). With preincrement, first the variable is incremented then the result is assigned. And your problem is not with operator presedence. Let me explain you what happens for x step by step: 1. x is created in the memory and the value 10 is assigned 2. x is assigned to x, in other words value of x is set to value of x (so x is 10) 3. The value stored in x is incremented by 1 (so now x is 11) 4. x is printed to the stdout (the printed value is 11) In your code it seems as if you are trying to print the old value of x (the value before the increment). For this to happen you should use
z = x++; cout << "x = " << z << '\n';
instead of
x = x++; cout << "x = " << x << '\n';
This way you will be able to print the old value. To make the things quite clear 1. x is created in the memory and the value 10 is assigned 2. x is assigned to z, in other words value of z is set to value of x (so z is 10) 3. The value stored in x is incremented by 1 (so now x is 11) 4. z is printed to the stdout (the printed value is 10).
I hope my explanation contains the answer to your question.
[del]
-- Sedat Dogru __________________________________________________ blue tec IT services ag
Sedat Dogru Project Manager
Kleinhadernerstr. 40 80689 München Germany
+49 - 89 - 700 76 43- 46
mail: sdogru@blue-tec.net internet: www.blue-tec.net
-- GMX - Die Kommunikationsplattform im Internet. http://www.gmx.net
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/
participants (4)
-
Jerry Feldman
-
John Lamb
-
Sedat DOÐRU
-
Verdi March