Thread: Output Problem

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Output Problem

    Hi.
    When I execute this program I get output of j as 20. Why it is not 21? Can anyone explain. Thanks.

    Code:
    int main(){
        int i =10;
        int j;
        j =  (i++ + i++);
        printf("%d",j);
        getch();
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    post increment (i++) changes the variable after the expression. the line before "j =..." i = 10, the line after assigning j, i has actually changed

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, the behaviour is undefined. 21 is also an acceptable output, but it so happens that you did not get it. Read this C FAQ on expressions. In particular, check out question 3.2.
    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

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Ok....

    But if i execute this program. It gives me 22 as output. Here it is possible because the value of i is changed within the expression. But in above case you are saying that it is 20 because i++ changes the variable after the expression. Please comment.

    Code:
    int main(){
        int i =10;
        int j;
       // j =  (i++ + i++);
       j = (i++ + ++i);
        printf("%d",j);
        getch();
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But if i execute this program. It gives me 22 as output. Here it is possible because the value of i is changed within the expression. But in above case you are saying that it is 20 because i++ changes the variable after the expression. Please comment.
    Both Lithorien and I have pointed out that there is undefined behaviour at work here. Any int value is a possible output, as is crashing, erasing your hard disk drive, launching a nuclear missle, etc.
    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

  7. #7
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by sunny_master_07 View Post
    Ok....

    But if i execute this program. It gives me 22 as output. Here it is possible because the value of i is changed within the expression. But in above case you are saying that it is 20 because i++ changes the variable after the expression. Please comment.

    Code:
    int main(){
        int i =10;
        int j;
       // j =  (i++ + i++);
       j = (i++ + ++i);
        printf("%d",j);
        getch();
    }
    It's entirely possible that your particular implementation has decided that "i++ + ++i" is treated like PHP, and is doing a right-to-left evaluation of that statement. Meaning, it increments I (because of the right hand operation), adds them, and then increments i again (because of the left hand operation). I can't promise you that, but that's my best-guess.

    HOWEVER.

    Relying on ANY undefined operation to do work is a horrible idea. Your program can act differently between compilers, operating systems, or hell, even a different version of the same compiler! It makes your code instantly non-portable, and is just an all around bad, bad idea. Avoid it.

  8. #8
    Banned
    Join Date
    Nov 2007
    Posts
    678
    if it helps to know, in Java the answer is 21, as OP expected!

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by manav View Post
    if it helps to know, in Java the answer is 21, as OP expected!
    ...Yes, and that's Java. Not C. Different rules in effect.

  10. #10
    Banned
    Join Date
    Nov 2007
    Posts
    678
    there are same rules, in both, Java and C (and C++ also)
    Code:
    i++; // return the value of i, then increment
    ++i; // increment the value of i, then return

  11. #11
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by manav View Post
    there are same rules, in both, Java and C (and C++ also)
    Code:
    i++; // return the value of i, then increment
    ++i; // increment the value of i, then return
    And yet... http://forum.java.sun.com/thread.jsp...sageID=9589354

    Similar problem, different results. Just because one implementation of one language does it one way doesn't mean it'll be done that way across all implementations of all languages where the semantics are the same.

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    `'.,~bIgMeSs~.'`

    such nerdy behavior is always annoying to me!

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Similar problem, different results. Just because one implementation of one language does it one way doesn't mean it'll be done that way across all implementations of all languages where the semantics are the same.
    I believe that Java specifies the precise evaluation order in this case. Of course, because it makes code more difficult to read, it should still be avoided in Java.
    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

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    hello it is defends on Compiler...

    Quote Originally Posted by sunny_master_07 View Post
    Hi.
    When I execute this program I get output of j as 20. Why it is not 21? Can anyone explain. Thanks.

    Code:
    int main(){
        int i =10;
        int j;
        j =  (i++ + i++);
        printf("%d",j);
        getch();
    }
    Becasue:-
    1.The bahavior undefined and completly defends on which compiler you are using??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my output
    By Dogmasur in forum C Programming
    Replies: 17
    Last Post: 08-07-2008, 08:07 PM
  2. output from bank type problem
    By IzaakF in forum C Programming
    Replies: 2
    Last Post: 09-04-2002, 06:42 PM
  3. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 07:36 AM
  4. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 08:32 PM