读书人

C语言 之 细节1

发布时间: 2012-08-22 09:50:34 作者: rapoo

C语言 之 细节一
这三行输出应该是什么?

#include <stdio.h>
int main()
{
int i;
i = 10;
printf("%d\n", i);
printf("%d\n", sizeof(i++));
printf("%d\n", i);
return 0;
}


求解释啊!

[解决办法]

C/C++ code
    printf("%d\n", sizeof(i++));00901410  mov         esi,esp  00901412  push        4  //直接是4,sizeof不是函数00901414  push        905858h  00901419  call        dword ptr ds:[9092BCh]  0090141F  add         esp,8  00901422  cmp         esi,esp  00901424  call        __RTC_CheckEsp (0901136h)
[解决办法]
C++标准:

5.3.3 sizeof
The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.”

也就是说,如果sizeof的操作数是一个表达式的话,这个表达式时不会被计算的。
另外一个操作符typeid也是如此。

读书人网 >C语言

热点推荐