C: What is the difference between ++i and i++?

前端 未结 21 2111
伪装坚强ぢ
伪装坚强ぢ 2020-11-21 06:04

In C, what is the difference between using ++i and i++, and which should be used in the incrementation block of a for loop?

21条回答
  •  花落未央
    2020-11-21 06:12

    ++i: is pre-increment the other is post-increment.

    i++: gets the element and then increments it.
    ++i: increments i and then returns the element.

    Example:

    int i = 0;
    printf("i: %d\n", i);
    printf("i++: %d\n", i++);
    printf("++i: %d\n", ++i);
    

    Output:

    i: 0
    i++: 0
    ++i: 2
    

提交回复
热议问题