What is the purpose of the pre-increment statement in C?

In C and many of its derivatives, i++ increases i and evaluates the value of i before it was increased, while ++i increases i and estimates the value of i after it was increased.

I can see the arguments for a particular increment operator; Many processors had a special increment operation code at that time, which was faster than adding, and conceptually “increment” represents a different idea from “add”, theoretically, if you write them differently, it can make the code more readable.

What I don't understand is a necessity for the pre-increment statement. Could such a practical use be written like this?

 #This... x = i++; #becomes this: x = i; ++i; 

Is there a historical reason that I don’t know about, maybe? Could you not throw away returned operator values ​​in the original versions of C?

+7
c ++ c language-design
source share
2 answers

One reason is that it allows you to generate efficient code without any optimization phases in compilers, provided that the programmer knew what he (or she) was doing. For example, when copying characters from one buffer to another, you can:

 register char *ptr1; register char *ptr2; ... for ( ... ) { *ptr1++ = *ptr2++; /* post-increment */ } 

The compiler I used to work with (on my own minicomputer) would generate the following register operations for assignment:

 load $r1,*$a1++ // load $r1 from address in $a1 and increment $a1 store $r1,*$a2++ // store $r1 at address in $a2 and increment $a2 

I forget the actual transaction codes. The compiler did not contain an optimization phase, but the code that was generated was very dense, provided that you understood the compiler and the architecture of the machine. He could do this because the hardware architecture had addressing modes before decrement and post-incremental processing for both address registers and general registers. As far as I remember, there were no preset and post-decrement addressing modes, but you could do without them.

I believe that the DEC mini-computers on which C was originally developed had these addressing modes. The machine I was working on was not made by DEC, but the architecture was pretty similar.

An optimization phase has been planned for the compiler. However, it was mainly used by system programmers, and when they saw how good the generated code was, the implementation of the optimization phase was quietly postponed.

The whole rationale for the C design was to allow the creation of simple and portable compilers that would generate reasonably efficient code with minimal (or missing) intermediate code optimization. For this reason, increment and decrement operators, as well as compound assignment operators, played an important role in the generation of compact and efficient code by the early C compilers. They were not just syntactic sugar, as suggested by Nicklaus Wirth et al.

+12
source share

So you can, for example, do it

 While (++i < threshold) [do something]; 

and this...

 While (i++ < threshold) [do something]; 

or any of a thousand other specific implementations that use the value and increase it in one expression and get the expected different results

-2
source share

All Articles