为什么 sizeof(x++) 不增加 x? [英] Why does sizeof(x++) not increment x?

查看:35
本文介绍了为什么 sizeof(x++) 不增加 x?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is the code compiled in dev c++ windows:

#include <stdio.h>

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

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

I expect x to be 6 after executing note 1. However, the output is:

4 and 5

谁能解释为什么 xnote 1 之后没有增加?

Can anyone explain why x does not increment after note 1?

推荐答案

来自 C99 标准(重点是我的)

6.5.3.4/2

sizeof 运算符产生其操作数的大小(以字节为单位),它可以是表达式或括号内的类型名称.大小由操作数的类型决定.结果是一个整数.如果操作数的类型是变长数组类型,则对操作数求值;否则,不计算操作数,结果是一个整数常量.

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.

这篇关于为什么 sizeof(x++) 不增加 x?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆