Different output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimhakans
    New Member
    • Sep 2007
    • 41

    Different output

    Hello there. I need help with this program.
    Code:
    { 
    int a=10,b;
    b=a++ + ++a;
    printf("%d,%d,%d,%d",b,a++,a,++a);
    }
    The output of this program is 22,13,13,13. But in the printf statement "a++" should print 11 instead of 13, "a" should print 10 instead of 13 and "++a" should print 11 instead of 13. That is what i can make from this program. Can anybody explain me this code?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    When you invoke the ++ operator (either pre- or post-increment) twice in the same statement, you invoke undefined behavior. How your compiler decides to resolve that is completely compiler-dependent. I'd be lucky to get the same output on my compiler.

    Comment

    • jimhakans
      New Member
      • Sep 2007
      • 41

      #3
      Ok but if i reduces that program to this
      Code:
      {
      int a=10;
      printf("%d,%d",a,a++);
      }
      In this program my compiler is printing 11 first and then 10. Why?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by jimhakans
        Ok but if i reduces that program to this
        Code:
        {
        int a=10;
        printf("%d,%d",a,a++);
        }
        In this program my compiler is printing 11 first and then 10. Why?
        There's a paragraph in the C and C++ Standard that deals with "undefined behaviour".
        Being dependent on the order of evaluation of your parameters simply induces
        that undefined behaviour. Some compilers evaluate actual function parameters
        from left to right; other evaluate them from right to left. You can't depend on it.

        kind regards,

        Jos

        Comment

        • jimhakans
          New Member
          • Sep 2007
          • 41

          #5
          Originally posted by JosAH
          There's a paragraph in the C and C++ Standard that deals with "undefined behaviour".
          Being dependent on the order of evaluation of your parameters simply induces
          that undefined behaviour. Some compilers evaluate actual function parameters
          from left to right; other evaluate them from right to left. You can't depend on it.

          kind regards,

          Jos
          what if i want "a" to be print first, then what should i do?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by jimhakans
            what if i want "a" to be print first, then what should i do?
            Use two printf() calls.

            Comment

            • kky2k
              New Member
              • May 2007
              • 34

              #7
              Originally posted by Ganon11
              When you invoke the ++ operator (either pre- or post-increment) twice in the same statement, you invoke undefined behavior. How your compiler decides to resolve that is completely compiler-dependent. I'd be lucky to get the same output on my compiler.
              refer this you will get the whole idea....

              Comment

              Working...