Thread: Increment operator

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    Increment operator

    Hi all,

    Code:
        int x=20,y=35; 
        x = y++ + x++;
    The value of x after the execution is 56. But from the looks of it i would expect it to be 55. But it seems the x++ (post increment operator) got applied before assigning value back to x. But i'm trying to work around this problem, how could x++ be executed before '=' operator. It seems to me 'x =' is totally a different statment to 'y++ + x++'?

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What you are seeing is the result of undefined behavior. See these links on Sequence Points and Sequence Points WIKI.

    Jim

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Ahh Thanks very much for the links. That makes sense

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1500 posts, and nearly 6 years on the board, and you're only just figuring this out?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >1500 posts, and nearly 6 years on the board, and you're only just figuring this out?
    A valid question, perhaps i'm not programming enough and my job donst involve much of development sadly :-/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your Location is giving you a compile time error about 'fseek' having too few arguments. (And bugging the pedantic in me!)


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    0 errors 0 warnings now

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    That was bugging me too. Thank jeebus you saw fit to curtial your maverick ways.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by adeyblue View Post
    That was bugging me too. Thank jeebus you saw fit to curtial your maverick ways.
    I find your attempt uninspired and lacking in originality. Also you clearly have no idea why it bugged me, as is reflected in your, dare I use the word, effort. I will however give you a passing grade of a D, just so that I don't have to see you attempt this course again.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment Operator
    By theunderdog118 in forum C Programming
    Replies: 4
    Last Post: 05-31-2010, 11:00 AM
  2. increment operator misbehaving
    By kingofudupi in forum C Programming
    Replies: 8
    Last Post: 09-28-2009, 05:24 AM
  3. Increment Operator
    By anirban in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 08:25 AM
  4. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 11:06 PM
  5. Help with increment operator, please
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-18-2008, 07:06 PM