what is difference between ++i and i+=1 from any point of view

前端 未结 7 508
一个人的身影
一个人的身影 2021-01-03 06:10

This is a question from kn king\'s c programming : a modern approach. I can\'t understand the solution given by him:-

The expression ++i is equivalent to (i          


        
7条回答
  •  清酒与你
    2021-01-03 06:41

    The solution means to say that there is no difference, ++i has the same meaning as (i += 1) no matter what i happens to be and no matter the context of the expression. The parentheses around i += 1 make sure that the equivalence holds even when the context contains further arithmetics, such as ++i * 3 being equivalent to (i += 1) * 3, but not to i += 1 * 3 (which is equivalent to i += 3).

    The same would not apply to i++, which has the same side effect (incrementing i), but a different value in the surrounding expression — the value of i before being incremented.

提交回复
热议问题