Thread: evaluation of the increment and decrement operators in C?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Unhappy evaluation of the increment and decrement operators in C?

    Code:
    printf("%d %d %d \n",++i,i++,--i);
    Can u please tell me how do we obtain the result as
    6 4 6....
    Ofcourse , its been evaluated using a stack...but please tell me how...with an explanation.
    I am not able to understand the evaluation method...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Order of evaluation of function arguments is implementation defined. Furthermore, the arguments in your example are evaluated in between consecutive sequence points, hence you are modifying i more than once between consecutive sequence points, resulting in undefined behaviour.
    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

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. confusion with increment and decrement operators
    By cBegginer in forum C Programming
    Replies: 6
    Last Post: 03-19-2005, 03:45 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. Replies: 11
    Last Post: 08-30-2004, 03:56 PM
  5. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM