#include <stdio.h> int main() { int i; i = 10; printf("%d/n", i); printf("%d/n", sizeof(i++)); printf("%d/n", i); return 0; }
這三行輸出應(yīng)該是什么? 答案是: 10 4 10 第三個(gè)為什么不是11? i為什么沒(méi)有自增? 請(qǐng)看C++標(biāo)準(zhǔn); 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. 也就是說(shuō),如果sizeof的操作數(shù)是一個(gè)表達(dá)式的話,這個(gè)表達(dá)式時(shí)不會(huì)被計(jì)算的。 sizeof當(dāng)預(yù)處理看就行了,它后面括號(hào)里的東西,根本不求值,只根據(jù)C的一堆規(guī)則判斷結(jié)果類型,然后返回結(jié)果類型的大小 另外一個(gè)操作符typeid也是如此。