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

前端 未结 21 1948
伪装坚强ぢ
伪装坚强ぢ 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:16

    ++i increments the value, then returns it.

    i++ returns the value, and then increments it.

    It's a subtle difference.

    For a for loop, use ++i, as it's slightly faster. i++ will create an extra copy that just gets thrown away.

提交回复
热议问题