Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
main()
{
	int i=2;
	printf("\n%d %d",++i,++i);
	getch();

}

here i do on turboC and i getting output 4,3
please explain why

What I have tried:

but according to me
++i so i incremented and printed
so i think that it must be 3,4 to be output
Posted
Updated 11-Nov-17 19:15pm
Comments
PIEBALDconsult 12-Nov-17 0:34am    
https://www.codeproject.com/Articles/1208666/Why-does-x-equals-plusplusx-plus-xplusplus-give-me
Afzaal Ahmad Zeeshan 12-Nov-17 9:32am    
Virtual 5. I was looking for this article to be shared.

You are in a gray zone, the resulting code is the compiler choice and thus it is unpredictable.
The only advice is: never write code with more than 1 increment on same variable in single line of code.
This code:
C++
#include<stdio.h>
main()
{
	int i=2;
	printf("\n%d %d",++i,++i);
	getch();
}

can translate to:
C++
#include<stdio.h>
main()
{
	int i=2;
	int t1=++i;
	int t2=++i;
	printf("\n%d %d",t1,t2);
	getch();
}

or to:
can translate to:
C++
#include<stdio.h>
main()
{
	int i=2;
	int t1=++i;
	int t2=++i;
	printf("\n%d %d",t2,t1);
	getch();
}

It is only compiler choice.
 
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