Output Problem

This is a discussion on Output Problem within the C Programming forums, part of the General Programming Boards category; Hi. When I execute this program I get output of j as 20. Why it is not 21? Can anyone ...

  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
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,712
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. File input output problem
    By VanJay011379 in forum C++ Programming
    Replies: 2
    Last Post: 08-30-2002, 02:09 PM
  4. String Output Problem
    By Yin in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2002, 06:36 AM
  5. Problem with output
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 12-11-2001, 07:32 PM

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