7

What is the historic reasoning behind having both prefix increment(++i) and postfix increment( i++) in the C language?

(this question and this other stack overflow question aren't as complete as the answers here.)

Two obvious ones always come up:

  1. One is often clearer than the other:

    For instance:

    i=0;
    
    while (i++ < 1) {
    do_it(i);
    }

    Executes only once, sure, but for i=1, not even something that actually satisfies the loop invariant because it's incremented AFTER the comparison. ++i seems a bit more clear there, though for is the better loop, here.

  2. The PDP-11 is often raised up as having an increment operator which was used to save an instruction or two over the code to increment with just generic addition.

Is there some other historic reason to offer both in the C language?

BenPen
  • 514
  • 3
  • 8
  • FWIW, the SO question that spawned this was a duplicate of an older question: http://stackoverflow.com/q/30429753/103167 – Ben Voigt Sep 24 '16 at 02:29
  • 1
    And it appears that here it is a duplicate as well, of http://programmers.stackexchange.com/q/43067/7757 – Ben Voigt Sep 24 '16 at 02:30

0 Answers0