The post increment operator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jayanth1987
    New Member
    • Feb 2007
    • 1

    The post increment operator

    I have a basic doubt...
    When we use the post incrment operator the incremnet of the
    variable is done after the execution of the statemnt?????
    for eg
    int i=1;
    printf("%d %d",i++,i++);

    shud the answer be 1 1
    or 1 2

    i want to understand the execution pt
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by Jayanth1987
    I have a basic doubt...
    When we use the post incrment operator the incremnet of the
    variable is done after the execution of the statemnt?????
    for eg
    int i=1;
    printf("%d %d",i++,i++);

    shud the answer be 1 1
    or 1 2

    i want to understand the execution pt
    the problem is that you don't know when the ++ operations are carried out (it is side effect) - they may done immediatly after the operand is used or saved until the end of the statement. For example, gcc and Borland CBuilder V5 gave
    2 1

    whereas Visual C 5.0 gave
    1 1

    in all cases the final value of i was 3

    in general the advice is do not use a variable more than once in an expression if one (or more) of the references has a ++ or -- attached to it.

    Comment

    • willakawill
      Top Contributor
      • Oct 2006
      • 1646

      #3
      Originally posted by Jayanth1987
      I have a basic doubt...
      When we use the post incrment operator the incremnet of the
      variable is done after the execution of the statemnt?????
      for eg
      int i=1;
      printf("%d %d",i++,i++);

      shud the answer be 1 1
      or 1 2

      i want to understand the execution pt
      Hi. I am getting more and more sensitive to the possibility that these questions are taken from college studies.

      It is only necessary to run this code to discover the output. You can run it several times to follow how the output changes. You can insert a pre-increment, ++i, to see how that effects the order of things.

      Test it and see. That is the fastest way to learn.

      Comment

      • Sebouh
        New Member
        • Feb 2007
        • 77

        #4
        I'm surprised Visual C gave 1 1. I think it should be 1 2, cause this statement is broken down when the compiler generates the object file. So it's like saying:
        print i : 1
        i = i + 1
        print i : 2
        i = i + 1

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by Sebouh
          I'm surprised Visual C gave 1 1. I think it should be 1 2, cause this statement is broken down when the compiler generates the object file. So it's like saying:
          print i : 1
          i = i + 1
          print i : 2
          i = i + 1
          try it with different compilers and see what you get but remember the order of evaluation of operands of individual operators and the order in which side effects take place is unspecified in C / C++

          see the last line of

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Originally posted by horace1
            try it with different compilers and see what you get but remember the order of evaluation of operands of individual operators and the order in which side effects take place is unspecified in C / C++

            see the last line of
            http://www.cppreference.com/operator_precedence.html
            Right. And vc++ 6 seems to process the parameters for printf in reverse order, i.e. right to left which is another consideration when working out the result of this syntax.

            Comment

            Working...