Thread: wat is the logic behind this?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12

    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?

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Mmm...homework.

    Think about it...it ain't rocket surgery. What would you have expected it to be, and why?

  3. #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?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    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

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    i expected 10 , bcz 'i'(RHS) gets incremented after assignin to 'i'( LHS)

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Just break it up into steps:

    Code:
    int i = 10; 
    i = i;
    ++i; 
    printf( "%d", i );
    Make sense?

  7. #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.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    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).

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    its of course 12......

  12. #12
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    sry, i'm not fully satisfied with ur ans,,, i think both or not same in tat case...

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

  15. #15
    Registered User
    Join Date
    Nov 2009
    Location
    Mysore, Karnataka, India
    Posts
    12
    i mean i=i++ can't be divided as i=i and ++i

Popular pages Recent additions subscribe to a feed

Similar Threads

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