Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
int k, n = 2;
k = (k = n + 5)++;
cout << k << '\n';
k = ++(k = n + 5);
cout << k << '\n';


I thought the 1st one is 7 and the 2nd is 8. But the output is surprisingly both 8.
Anyone can tell me about this? Thx in advance.
Posted
Comments
Krauze 20-Apr-12 7:48am    
Thanks, P. Salini
P.Salini 20-Apr-12 7:53am    
welcome Krauze

In C++, it is undefined as is i = i++ since i is modified twice in the same expression and the order is not defined as operator= is not a sequence point.

See http://en.wikipedia.org/wiki/Sequence_point[^]
 
Share this answer
 
Comments
Lakamraju Raghuram 21-Apr-12 9:13am    
I agree with this.
Statements like k = (k = n + 5)++; looks fancy but no serious developer should not ever use them as many ambitiousness are involved
[no name] 21-Apr-12 9:27am    
Are you saying that the result of k = (k = n + 5)++; is undefined? Don't the enclosing brackets define the order of evaluation?
Philippe Mori 21-Apr-12 9:57am    
Yes, check the Wikipedia link I gave above. Essentially, the compiler will not take into account that it is the same variable k that is modified twice in the same expression. Thus the compiler might store the result of k=n+5 in a temporary variable. Increment k and do the assigment or assign the result twice to k before doing the increment for example. It will give the expected result except in the case where the same variable is modified multiple time (either directly or through an alias) where the result is undefined (but in practice, it will probably be either 7 or 8).
[no name] 21-Apr-12 10:06am    
I agree with another poster that it is not nice to see this kind of expression. But if it is dangerous why doesn't the compiler issue a warning? It is very easy to pick up this condition.
Philippe Mori 21-Apr-12 10:51am    
Well, simple cases would be easy for the compiler to give a warning... but the you just have to suppose you have a function say f(int &i, int &j, int &k) { i = ++j + k++; }. It would normally not be a problem except if the caller call it with the same variable for multiple argument in which case you will still have the problem.

By the way, expression like k = i++ + ++i are also undefined as it is not defined which subexpression is evaluated first (and when intermediate results are loaded and stored).
The 1st statement also returning 8 because
when the execution of this statement is completed
k = (k = n + 5)++;
then the postincrement also takes place and you are getting result as 8.

if you print (k = n + 5)++ then the output will be 7.
 
Share this answer
 
Comments
sravani.v 20-Apr-12 7:53am    
my 5!
P.Salini 20-Apr-12 7:54am    
Thank you sravani
Prasad_Kulkarni 20-Apr-12 8:04am    
Good one +5!
P.Salini 20-Apr-12 23:46pm    
Thank you Prasad
Philippe Mori 21-Apr-12 9:03am    
This explanation is not correct. See my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900