Like Tree1Likes
  • 1 Post By jimblumberg

Increment operator

This is a discussion on Increment operator within the C Programming forums, part of the General Programming Boards category; Hi all, Code: int x=20,y=35; x = y++ + x++; The value of x after the execution is 56. But ...

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

    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
    2,755
    What you are seeing is the result of undefined behavior. See these links on Sequence Points and Sequence Points WIKI.

    Jim
    ssharish2005 likes this.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    >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,682
    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
    611
    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, 07:25 AM
  4. Help with increment operator
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 10:06 PM
  5. Help with increment operator, please
    By capvirgo in forum C Programming
    Replies: 4
    Last Post: 02-18-2008, 06:06 PM

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