Thread: Help with increment operator, please

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Unhappy Help with increment operator, please

    Code:
    int main()
    {
      int i=3;
      int j;
    
      ++i + ++i;  
    
      printf("\n\ti=%d j=%d", i ,j);  //Prints  i = 5. But its (4)+(5) = 9 right?
    
      return 0;
    }
    Prints i = 5. But its (4)+(5) = 9 right?

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    okay, I think, since their is no assignment operator. summing of ++i and ++i does not happen. Right ?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by capvirgo View Post
    okay, I think, since their is no assignment operator. summing of ++i and ++i does not happen. Right ?
    The only thing that is safe to say is that the expression "++i + ++i" is syntactically correct but semantically meaningless. It is not allowed to assume that the value of i is anything in particular, or that it even exists anymore. It is likely that since the expression was not assigned to j, the only thing that happened were the side effects (i was incremented twice, giving a value of 5), and no addition occurred.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    What would be kind of cool is the ability to play a sound file of a guy with a good radio voice going, "Ladies and gentlemen..... Welcome to UNDEFINED BEHAVIOR!"

    I think such a warning might be appropriate in this topic in the near future, if not right now.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  2. How do I increment printf hex characters?
    By gechno in forum C Programming
    Replies: 2
    Last Post: 05-24-2004, 05:33 PM
  3. increment IP addresses - newbie Q.
    By webwesen in forum C Programming
    Replies: 6
    Last Post: 09-09-2003, 08:25 AM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:01 AM