i++, ++i, i+=1 and i = i+1;

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • malc

    #16
    Re: i++, ++i, i+=1 and i = i+1;

    Ben Pfaff <blp@cs.stanfor d.eduwrites:
    malc <malc@pulsesoft .comwrites:
    >
    >an appeal to authority is not a right thing to do.
    >
    I agree.
    Heh, clever.

    --
    vale

    Comment

    • J. J. Farrell

      #17
      Re: i++, ++i, i+=1 and i = i+1;


      krister@kallean ka.se wrote:
      "jim" <jimyoo1@gmail. comwrote in message
      news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...

      1) i++; /* use i and increment by one */
      2) ++i; /* increment i by one and use it */
      3) i += 1;
      4) i = i+1;

      result (for value of i) of all 4 will be same; could anyone tell
      differences among them from any perspectives?
      I heard we'd better use 2) over 1). And 1) is faster than 3) or 4).
      >
      I was told that in case 3) "i" is accessed once and modified once;
      in case 4) "i" is accessed twice and modified once.
      I recommend you stop listening to whoever told you that.
      Someone else could hopefully say if it is true and what it means in reality.
      It may be true, the compiler's free to implement it that way if it
      likes. It's also free in case 3 (and in the other cases) to read it 20
      times and write it 43 times if it so chooses. Any compiler which
      generates significantly different code for these four statements is of
      very poor quality. They should all compile to the same code sequence,
      which increments i using whatever sequence of operations is most
      efficient.

      Comment

      • pete

        #18
        Re: i++, ++i, i+=1 and i = i+1;

        J. J. Farrell wrote:
        >
        krister@kallean ka.se wrote:
        "jim" <jimyoo1@gmail. comwrote in message
        news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...
        >
        1) i++; /* use i and increment by one */
        2) ++i; /* increment i by one and use it */
        3) i += 1;
        4) i = i+1;
        I was told that in case 3) "i" is accessed once and modified once;
        in case 4) "i" is accessed twice and modified once.
        >
        I recommend you stop listening to whoever told you that.
        I disagree.
        Someone else could hopefully say
        if it is true and what it means in reality.
        >
        It may be true,
        It is true.
        They should all compile to the same code sequence,
        which increments i using whatever sequence of operations is most
        efficient.
        That all depends on whether or not (i) is an expression
        with side effects.

        /* BEGIN new.c ouput */

        After: array[0] = 0; array[1] = 7; counter = 0; ++i;

        i is 7
        array[0] is 1
        array[1] is 7

        After: array[0] = 0; array[1] = 7; counter = 0; i = i + 1;

        i is 0
        array[0] is 0
        array[1] is 1

        /* END new.c ouput */



        /* BEGIN new.c */

        #include <stdio.h>

        #define i (*side_effects( array))

        static unsigned counter;

        int *side_effects(i nt *array);

        int main(void)
        {
        int array[2];

        puts("/* BEGIN new.c ouput */\n");
        array[0] = 0;
        array[1] = 7;
        counter = 0;
        ++i;
        printf("After: array[0] = 0;"
        " array[1] = 7; counter = 0; ++i;\n\n");
        printf("i is %d\n", i);
        printf("array[0] is %d\n", array[0]);
        printf("array[1] is %d\n\n", array[1]);
        array[0] = 0;
        array[1] = 7;
        counter = 0;
        i = i + 1;
        printf("After: array[0] = 0;"
        " array[1] = 7; counter = 0; i = i + 1;\n\n");
        printf("i is %d\n", i);
        printf("array[0] is %d\n", array[0]);
        printf("array[1] is %d\n", array[1]);
        puts("\n/* END new.c ouput */");
        return 0;
        }

        int *side_effects(i nt *array)
        {
        return array + counter++ % 2;
        }

        /* END new.c */

        --
        pete

        Comment

        • J. J. Farrell

          #19
          Re: i++, ++i, i+=1 and i = i+1;


          pete wrote:
          J. J. Farrell wrote:

          krister@kallean ka.se wrote:
          "jim" <jimyoo1@gmail. comwrote in message
          news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...

          1) i++; /* use i and increment by one */
          2) ++i; /* increment i by one and use it */
          3) i += 1;
          4) i = i+1;
          >
          I was told that in case 3) "i" is accessed once and modified once;
          in case 4) "i" is accessed twice and modified once.
          I recommend you stop listening to whoever told you that.
          >
          I disagree.
          >
          Someone else could hopefully say
          if it is true and what it means in reality.
          It may be true,
          >
          It is true.
          It may or may not be true; the "as if" rule says so. With anything that
          deserves the name of "C compiler" in the case that the OP was asking
          about, it is not true.
          They should all compile to the same code sequence,
          which increments i using whatever sequence of operations is most
          efficient.
          >
          That all depends on whether or not (i) is an expression
          with side effects.
          Indeed, though it's clear from the OP's posting that he was asking
          about the simple case.
          /* BEGIN new.c ouput */
          >
          After: array[0] = 0; array[1] = 7; counter = 0; ++i;
          >
          i is 7
          array[0] is 1
          array[1] is 7
          >
          After: array[0] = 0; array[1] = 7; counter = 0; i = i + 1;
          >
          i is 0
          array[0] is 0
          array[1] is 1
          >
          /* END new.c ouput */
          In his posting, the OP defined that the end value of i is identical in
          all four cases, so your example is not relevant to the question in hand.

          Comment

          • pete

            #20
            Re: i++, ++i, i+=1 and i = i+1;

            J. J. Farrell wrote:
            >
            pete wrote:
            J. J. Farrell wrote:
            >
            krister@kallean ka.se wrote:
            "jim" <jimyoo1@gmail. comwrote in message
            news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...
            >
            1) i++; /* use i and increment by one */
            2) ++i; /* increment i by one and use it */
            3) i += 1;
            4) i = i+1;
            I was told that in case 3) "i" is accessed once and modified once;
            in case 4) "i" is accessed twice and modified once.
            >
            I recommend you stop listening to whoever told you that.
            I disagree.
            Someone else could hopefully say
            if it is true and what it means in reality.
            >
            It may be true,
            It is true.
            >
            It may or may not be true;
            the "as if" rule says so. With anything that
            deserves the name of "C compiler" in the case that the OP was asking
            about, it is not true.
            >
            They should all compile to the same code sequence,
            which increments i using whatever sequence of operations is most
            efficient.
            That all depends on whether or not (i) is an expression
            with side effects.
            >
            Indeed, though it's clear from the OP's posting that he was asking
            about the simple case.
            It's clear from
            "I was told that in case 3) "i" is accessed once and modified once;
            in case 4) "i" is accessed twice and modified once."
            that what he was being told,
            was taking the non simple case into account.

            /* BEGIN new.c ouput */

            After: array[0] = 0; array[1] = 7; counter = 0; ++i;

            i is 7
            array[0] is 1
            array[1] is 7

            After: array[0] = 0; array[1] = 7; counter = 0; i = i + 1;

            i is 0
            array[0] is 0
            array[1] is 1

            /* END new.c ouput */
            >
            In his posting, the OP defined that the end value of i is identical in
            all four cases,
            so your example is not relevant to the question in hand.
            In the example I that gave
            the value of i was 1, prior to ++i, and
            the value of i was 1, prior to i = i + 1.

            --
            pete

            Comment

            • pete

              #21
              Re: i++, ++i, i+=1 and i = i+1;

              pete wrote:
              >
              J. J. Farrell wrote:

              pete wrote:
              J. J. Farrell wrote:

              krister@kallean ka.se wrote:
              "jim" <jimyoo1@gmail. comwrote in message
              news:1166556524 .397567.30780@t 46g2000cwa.goog legroups.com...

              1) i++; /* use i and increment by one */
              2) ++i; /* increment i by one and use it */
              3) i += 1;
              4) i = i+1;
              >
              I was told that in case 3) "i" is accessed once and modified once;
              in case 4) "i" is accessed twice and modified once.

              I recommend you stop listening to whoever told you that.
              >
              I disagree.
              >
              Someone else could hopefully say
              if it is true and what it means in reality.

              It may be true,
              >
              It is true.
              It may or may not be true;
              the "as if" rule says so. With anything that
              deserves the name of "C compiler" in the case that the OP was asking
              about, it is not true.
              They should all compile to the same code sequence,
              which increments i using whatever sequence of operations is most
              efficient.
              >
              That all depends on whether or not (i) is an expression
              with side effects.
              Indeed, though it's clear from the OP's posting that he was asking
              about the simple case.
              >
              It's clear from
              "I was told that in case 3) "i" is accessed once and modified once;
              in case 4) "i" is accessed twice and modified once."
              that what he was being told,
              was taking the non simple case into account.
              >
              >
              /* BEGIN new.c ouput */
              >
              After: array[0] = 0; array[1] = 7; counter = 0; ++i;
              >
              i is 7
              array[0] is 1
              array[1] is 7
              >
              After: array[0] = 0; array[1] = 7; counter = 0; i = i + 1;
              >
              i is 0
              array[0] is 0
              array[1] is 1
              >
              /* END new.c ouput */
              In his posting, the OP defined that the end value of i is identical in
              all four cases,
              so your example is not relevant to the question in hand.
              >
              In the example I that gave
              the value of i was 1, prior to ++i, and
              the value of i was 1, prior to i = i + 1.
              I meant to say that the value of i was 0,
              prior to the assignment operation.

              Anyway,
              the comment about the difference bewteen case 3) and case 4),
              was about the difference in semantics
              between simple assignment and compound assignment,
              and was very nearly a quote from the standard.

              N869
              6.5.16.2 Compound assignment
              Semantics
              [#3] A compound assignment of the form E1 op= E2 differs
              from the simple assignment expression E1 = E1 op (E2) only
              in that the lvalue E1 is evaluated only once.


              --
              pete

              Comment

              Working...