Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
int main()


{
	int a,b;
	a=5;
	
	b=++a + a++;
	
	cout<<b;

	
	
	
}


What I have tried:

Quote:
why does not it come 12 instead of 13?
Posted
Updated 1-Sep-17 2:02am
Comments
Richard MacCutchan 1-Sep-17 7:19am    
It depends: the results are not guaranteed as the compiler writers may handle such expressions differently. Bottom line: never write expressions like that.
Member 12921311 1-Sep-17 7:26am    
okay sir, thank you.

Depends. It depends on the compiler you use, the version of the compiler, which way the wind is blowing, ...
The pre- and post-increment operators are defined to increase the value before or after the value is used - but nothing in the C or C++ specification says when that should occur. It could be before the line is executed at all, it could be after the line if finished with. It could even be immediately the command is used. To make things worse, the execution order within an expression is not defined either (except in specific cases involving && and || operators) - the compiler write it free to work right to left, or left to right, or top down, or bottom up, or ... you get the idea.
And optimisations may change all that, so your production compiled code may not work the same as your development code did.

So don't do things like that: the results are not guaranteed and cannot be relied upon - just because you can do something, does not mean you should!
 
Share this answer
 
 
Share this answer
 

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