復制代碼 代碼如下:
#include <stdio.h>
int main()
{
int i;
i = 10;
printf("%d\n", i);
printf("%d\n", sizeof(i++));
printf("%d\n", i);
return 0;
}
這三行輸出應該是什么?
答案是:
10
4
10
第三個為什么不是11? i為什么沒有自增?
請看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的操作數是一個表達式的話,這個表達式時不會被計算的。
sizeof當預處理看就行了,它后面括號里的東西,根本不求值,只根據C的一堆規則判斷結果類型,然后返回結果類型的大小
另外一個操作符typeid也是如此。