Thread: pre-increment operator..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    pre-increment operator..

    i wrote the following program and it's output is 14.

    Code:
    #include <stdio.h>
    
    
    main()
    {
      int i=5;
      printf("%d",++i + ++i);
    }
    How is the output 14...should'nt it be 13..??
    can someone explain..??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Read the various entries in this C FAQ on expressions.
    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

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    went through the FAQ but still cannot determine the logic in my program..
    can you please post a little explanation..

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In other words, you don't understand the FAQ that I linked to. Okay, here's a simplified version: what you are trying to do results in undefined behaviour, which is a Bad Thing. It results in undefined behaviour because you are trying to modify i twice between consecutive sequence points (in this case, within the same expression). Hence, there is no logic to explain: what you are doing is simply wrong. The compiler is perfectly fine in causing the output to be 14. It could have made the output 13, 12, or "hello world", and it would still be fine, because of the 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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    ok..thanks for the explanation..
    i read the FAQ which said absolutely the same thing..
    "The behaviour cannot be determined..."
    But it is the first time i came across this kind of question.
    so i was little perplexed..
    Any Way Thank you
    @laserlight..

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The order of evaluation of (++i + ++i) is not defined. In your case what happened is that both pre-increment operations were performed before doing the addition. Effectively what you I got the same result of 14 with 3 different versions of microsoft compilers, but with another compiler the result might be 13. In the case where the result is 14, the effect was the the same as (not really sure if this is defined, but it may be easier to understand):

    Code:
        printf("%d\n", i + (i = i + 2));
    Last edited by rcgldr; 05-02-2013 at 01:44 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by rcgldr
    The order of evaluation of (++i + ++i) is not defined.
    That is not accurate. The issue here is not about the order of evaluation. The issue here is that the behaviour is undefined.

    Here's a trick question: given the program that narendrav posted in post #1, which of these options labelled A to D is not a possible valid output of the program?
    A) 12
    B) 13
    C) 14
    D) 15

    Quote Originally Posted by rcgldr
    In the case where the result is 14, the effect was the the same as (not really sure if this is defined, but it may be easier to understand):
    Code:
    printf("%d\n", i + (i = i + 2));
    The C99 version rule is:
    Quote Originally Posted by C99 Clause 6.5 Paragraph 2
    Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
    Hence the behaviour is undefined because you modify i, yet you also read i for a purpose other than to determine the value to be stored.
    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 operator
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 06-06-2011, 04:32 PM
  2. Increment Operator
    By theunderdog118 in forum C Programming
    Replies: 4
    Last Post: 05-31-2010, 11:00 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