Printf Doubt

Status
Not open for further replies.

blueshift

Wise Old Crow
Can you please explain a bit more in detail, how we get 3 2 as output ?

Code:
int i=2;
printf("%d %d",i,i++);
When we code like this, the evaluation happens from right to left i.e. first i++ will be done. So for the 2nd %d in printf(), the value of i is still '2'. Then only i is incremented to '3' and displayed in the 1st %d. All this happens during compiling.
If instead the code is:
Code:
printf("%d %d", i++, i);
the result is: 2 2
If you use another printf() after this, then it will display i as '3'.
 
K

khattam_

Guest
It must give 3 2 as the output coz calaculations are done from right side in printf.
Check again.
I tried in Turbo and GCC.

sorry its
Code:
3 2

just a typing mistake.. hope you got what I was tryin to explain...
so you see your earlier explanation was wrong...



Anyways, I've got the answer when I asked in another forum.
*forum.mazzako.com/index.php?topic=13291.0
 
Status
Not open for further replies.
Top Bottom