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

前端 未结 7 553
一个人的身影
一个人的身影 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:22

    In a normal operation without assignation:

    ++i and i++

    increase the variable in 1. In pseudo assembly both code it is:

    inc i
    

    but If you assign the value the order of ++ is relevant:

    x = i++

    produce:

    mov x, i
    inc i
    

    x = ++i

    produce:

    inc i
    mov x, i
    

    In the case of: i += 1

    it will produce:

    add i,1
    

    but because compilers optimize the code it also will produce in this case:

    inc i
    

提交回复
热议问题