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

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

    Shortly:

    ++i and i++ works same if you are not writing them in a function. If you use something like function(i++) or function(++i) you can see the difference.

    function(++i) says first increment i by 1, after that put this i into the function with new value.

    function(i++) says put first i into the function after that increment i by 1.

    int i=4;
    printf("%d\n",pow(++i,2));//it prints 25 and i is 5 now
    i=4;
    printf("%d",pow(i++,2));//it prints 16 i is 5 now
    

提交回复
热议问题