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

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

    Here is the example to understand the difference

    int i=10;
    printf("%d %d",i++,++i);
    

    output: 10 12/11 11 (depending on the order of evaluation of arguments to the printf function, which varies across compilers and architectures)

    Explanation: i++->i is printed, and then increments. (Prints 10, but i will become 11) ++i->i value increments and prints the value. (Prints 12, and the value of i also 12)

提交回复
热议问题