What are the historical reasons C languages have pre-increments and post-increments?

后端 未结 6 978
广开言路
广开言路 2021-02-08 12:32

(Note: I am not asking about the definitions of pre-increment vs. post-increment, or how they are used in C/C++. Therefore, I do not think this is a duplicate question.)

6条回答
  •  死守一世寂寞
    2021-02-08 13:25

    When you count down from n it is very important whether is pre-decrement or post-decrement

    #include 
    void foopre(int n) {
        printf("pre");
        while (--n) printf(" %d", n);
        puts("");
    }
    void foopost(int n) {
        printf("post");
        while (n--) printf(" %d", n);
        puts("");
    }
    int main(void) {
        foopre(5);
        foopost(5);
        return 0;
    }
    

    See the code running at ideone.

提交回复
热议问题