gpt4 book ai didi

c - 为什么 sizeof(x++) 不增加 x?

转载 作者:太空狗 更新时间:2023-10-29 17:26:25 25 4
gpt4 key购买 nike

这是在 dev c++ windows 中编译的代码:

#include <stdio.h>

int main() {
int x = 5;
printf("%d and ", sizeof(x++)); // note 1
printf("%d\n", x); // note 2
return 0;
}

我希望 x 在执行 note 1 后为 6。但是,输出是:

4 and 5

谁能解释为什么 xnote 1 之后不递增?

最佳答案

来自C99 Standard (重点是我的)

6.5.3.4/2

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

关于c - 为什么 sizeof(x++) 不增加 x?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995680/

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