C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2009, 07:54 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 8
wat is the logic behind this?

i=10; i=i++; printf("%d",i);


for the above part of the prog i got output 11.... y is it? wat is the logic?
vpshastry is offline   Reply With Quote
Old 11-17-2009, 08:04 AM   #2
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 814
Mmm...homework.

Think about it...it ain't rocket surgery. What would you have expected it to be, and why?
rags_to_riches is offline   Reply With Quote
Old 11-17-2009, 08:14 AM   #3
Registered User
 
Join Date: Nov 2007
Posts: 11
Quote:
Originally Posted by vpshastry View Post
i=10; i=i++; printf("%d",i);


for the above part of the prog i got output 11.... y is it? wat is the logic?
Why don't you tell us what you think each of the three parts are doing?
binaryboy is offline   Reply With Quote
Old 11-18-2009, 06:13 AM   #4
Registered User
 
Join Date: Nov 2009
Posts: 8
i expected the output to be 10, 1st is assigni 10 to i, 2nd is incrementin 'i' after assignin it to i, 3rd is print,,,,, o thought a lot abt it.... atill i'm not gettin
vpshastry is offline   Reply With Quote
Old 11-18-2009, 06:15 AM   #5
Registered User
 
Join Date: Nov 2009
Posts: 8
i expected 10 , bcz 'i'(RHS) gets incremented after assignin to 'i'( LHS)
vpshastry is offline   Reply With Quote
Old 11-18-2009, 06:57 AM   #6
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 5,034
Just break it up into steps:

Code:
int i = 10; 
i = i;
++i; 
printf( "%d", i );
Make sense?
Sebastiani is offline   Reply With Quote
Old 11-18-2009, 07:27 AM   #7
Registered User
 
Join Date: Nov 2007
Posts: 11
Quote:
Originally Posted by vpshastry View Post
i expected the output to be 10, 1st is assigni 10 to i, 2nd is incrementin 'i' after assignin it to i, 3rd is print,,,,, o thought a lot abt it.... atill i'm not gettin
And that's where things are letting you down. Remember the right hand side is evaluated first and then assigned to whatever's on the left side of the equals sign.

Think about it, and consider that the following snippets are all going to do the same thing

i = i++;

i++;

i = i + 1;

etc.
binaryboy is offline   Reply With Quote
Old 11-18-2009, 09:58 AM   #8
Registered User
 
Join Date: Jan 2009
Posts: 30
The postfix operator '++' will allow 'i' to be assigned to itself and then it will increment it. Apparently it has a high precedence but it isn't actually incremented until last (C Operator Precedence Table).
DeadPlanet is offline   Reply With Quote
Old 11-18-2009, 12:42 PM   #9
Registered User
 
Join Date: Nov 2009
Posts: 77
you just claimed to know that 'i' was incrementing, and you still expect 'i' to equal the initial value, you're obviously lying or you don't know what the word 'increment' means.

i'll ask the question like this;

i = 10;
i = i(10) + 1

what is 10 + 1?

i = 10 + 1

i will give you a hint, 10 + 1 does not equal 10.

http://en.wikipedia.org/wiki/Increment

read^

pop quiz:

i = 10
i++
++i

what's i?

Last edited by since; 11-18-2009 at 12:47 PM.
since is offline   Reply With Quote
Old 11-18-2009, 12:59 PM   #10
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Slightly off topic, but I keep forgetting... does i = i++; result in undefined behaviour? It does seem to trigger that, except that in all reasonable interpretations of the expression it is equivalent to a standalone i++ so the problem is more theoretical than practical.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-22-2009, 02:27 AM   #11
Registered User
 
Join Date: Nov 2009
Posts: 8
its of course 12......
vpshastry is offline   Reply With Quote
Old 11-22-2009, 02:30 AM   #12
Registered User
 
Join Date: Nov 2009
Posts: 8
sry, i'm not fully satisfied with ur ans,,, i think both or not same in tat case...
vpshastry is offline   Reply With Quote
Old 11-22-2009, 02:32 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by vpshastry
sry, i'm not fully satisfied with ur ans,,, i think both or not same in tat case...
What do you mean?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-22-2009, 02:51 AM   #14
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 814
Laserlight is correct...it's classic undefined behavior. What happens is purely a consequence of what the compiler decides to do with it. See here for information on sequence points, which is pertinent to the discussion.

I apologize for my smart-ass answer in the beginning of the thread. Doing stupid things like i=i++ never crosses my mind, so I forget about it as being "undefined;" it simply makes no sense to do it in the first place.
rags_to_riches is offline   Reply With Quote
Old 12-10-2009, 12:20 PM   #15
Registered User
 
Join Date: Nov 2009
Posts: 8
i mean i=i++ can't be divided as i=i and ++i
vpshastry is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling logic and draw cycles DavidP Game Programming 1 07-25-2009 10:15 AM
Digital Logic strokebow Tech Board 3 12-09-2006 01:05 PM
wat is the use of sizeof(int)? zell C Programming 3 01-24-2005 05:27 AM
Circular Logic DavidP A Brief History of Cprogramming.com 1 10-15-2001 08:10 PM


All times are GMT -6. The time now is 12:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22