![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 8
| wat is the logic behind this? for the above part of the prog i got output 11.... y is it? wat is the logic? |
| vpshastry is offline | |
| | #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 | |
| | #3 |
| Registered User Join Date: Nov 2007
Posts: 11
| |
| binaryboy is offline | |
| | #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 | |
| | #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 | |
| | #6 |
| Guest Join Date: Aug 2001
Posts: 5,034
| Just break it up into steps: Code: int i = 10; i = i; ++i; printf( "%d", i ); |
| Sebastiani is offline | |
| | #7 | |
| Registered User Join Date: Nov 2007
Posts: 11
| Quote:
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 | |
| | #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 | |
| | #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 | |
| | #10 |
| C++ Witch 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 | |
| | #11 |
| Registered User Join Date: Nov 2009
Posts: 8
| its of course 12...... |
| vpshastry is offline | |
| | #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 | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
__________________ 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 | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |