See dear,
++i is pre incrementing while i++ is post incrementing.
So,
int i=5; - > this defines the variable to have a value 5;
printf("%d", ++i); = 6 - > Her the value of 'i' is post incremented so it
becomes '6'
printf("%d", i++); = 5 - > Here 'i' is post incremented so the value
should be '6' again
I dont know why you are getting '5'
Then if you add another printf like this
printf("%d", i++); - > Here you should get 7
Then again
printf("%d", ++i); - > This should give 9 instead of 8 as it's being
pre incremented
Do mention which compiler you are using. I am using Dev-C++ a vWindows
version of GCC
Regards,
Debasish
-------------------------------------------------------------------------------------------------------------------------------------
<html><DIV>
<P><FONT face="Lucida Handwriting, Cursive" color=#ff9900
size=2>Karmennavava Dhikaraste, Maaphaleshu
Kadaachanah</FONT></P></DIV></html>
From: Deepak Shankar <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: [email protected]
Subject: [c-prog] answer 4 ur question.....
Date: Mon, 5 Feb 2007 05:21:19 -0800 (PST)
No chance both the printf will print '6' only.....
[mod - I think you'll find that the OP was assuming one of the printf()
would be executed - not both in order which is what you're assuming - PJH]
-----Original Message-----
From:
[EMAIL PROTECTED] com [mailto:c-prog@ yahoogroups. com] On Behalf Of
sharma_anish9188
Sent: Sunday, February 04, 2007 8:09
PM
To: [EMAIL PROTECTED] com
Subject: [c-prog] help in
the logic
I cant understand the logic
can anybody help me to under
stand
int i=5;
printf("%d", ++i); = 6
printf("%d", i++);
= 5
how?
[mod - I assume you mean only one of those printf()'s,
not the two in series as you've presented it -
-------------------------------------------------------------------------------------------------
becoz ++a mean pre increment so it will increment then it will print ,but
whereas a++ is post increment so it will print the current value then it
will increment.......
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com
_________________________________________________________________
Share your story and get it featured on MSN
http://content.msn.co.in/Contribute/Default.aspx