2012-04-26 69 views
0

研究Ç轉換我發現了一個約整數促銷款,我沒有很明白,它說:關於整數促銷

"Integer types smaller than int are promoted when an operation is performed on them. If all values of the original type can be represented as an int, the value of the smaller type is converted to an int; otherwise, it is converted to an unsigned int"

考慮到:

  1. 的任何無符號整數類型的等級等於對應的有符號整數類型的等級

  2. long long int的等級大於long int的等級,該等級大於int的等級,該等級大於short int的等級,該等級大於signed char的等級。

問題是: 爲什麼小於int的類型不應該用int表示? 以及爲什麼無符號整數可以表示int不應該表示的值?

在此先感謝。

+0

你想知道爲什麼有人會更喜歡比int更短的東西嗎? – kevin628 2012-04-26 15:29:32

回答

0

雖然short具有較低的秩int,它可以是相同的尺寸(位),例如在16位系統上的sizeof(int) == sizeof(short) == 2。因此unsigned short可能能夠保存大於INT_MAX的值。

對於問題的第二部分,其答案大致相同:unsigned int可以保存不能表示爲int的值,即INT_MAX+1 .. UINT_MAX

+0

非常感謝,現在很清楚 – newbie 2012-04-26 15:35:45