sizeof(i++)

匿名 (未验证) 提交于 2019-12-03 00:26:01

朋友今天出了一道题

int main(void)

{




}

问i的值是多少,我想当然的问答是6。但实际运算结果是5,思考下第一反应应该是sizeof是编译时求值导致的。

先看下汇编结果:



.LC0:




main:
.LFB0:





















.LFE0:


看下标记1,实际上直接求出了i对应的数据类型值

这个应该是C标准规定的:

6.5.3.4 The sizeof operator

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在非可变长度的数组情况下操作数是不会被求值的


文章来源: sizeof(i++)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!