gpt4 book ai didi

c++ - 如果我在同一语句中使用预增量和后增量会怎样?

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:12 25 4
gpt4 key购买 nike

<分区>

我今天看到一个有趣的语句,有后增量和前增量。请考虑以下程序-

#include <stdio.h>

int main(){
int x, z;

x = 5;
z = x++ - 5; // increase the value of x after the statement completed.
printf("%d\n", z); // So the value here is 0. Simple.

x = 5;
z = 5 - ++x; // increase the value of x before the statement completed.
printf("%d\n", z); // So the value is -1.

// But, for these lines below..

x = 5;
z = x++ - ++x; // **The interesting statement
printf("%d\n", z); // It prints 0

return 0;
}

那个有趣的陈述实际上发生了什么?后增量应该在语句完成后增加 x 的值。然后 first x 的值对于该语句保持为 5。并且在预增量的情况下,second x 的值应该是 6 或 7(不确定)。

为什么给z赋值0?是 5 - 5 还是 6 - 6?请解释。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com