C語言裡的型別提公升

2021-07-29 10:06:32 字數 812 閱讀 4068

一、型別的提公升

把char、unsigned char、short、unsigned short轉換成int型別稱為型別提公升(promotion)。

1. 如果short的位元組長度小於int的位元組長度

char轉換成 int

unsigned char轉換成 int

short轉換成 int

unsigned short轉換成 int

2. 如果short的位元組長度等於int的位元組長度

char轉換成 int

unsigned char轉換成 int

short轉換成 int

unsigned short轉換成 unsigned int

二、型別的轉換

long double、double、float、unsigned long long、long long、unsigned long、long、unsigned int、int之間的轉換稱為型別轉換

1. 如果int的位元組長度小於long的位元組長度

型別等級由高到低依次為:long double、double、float、 unsigned long long、long long、unsigned long、long、unsigned int、int

2. 如果int的位元組長度等於long的位元組長度

型別等級由高到低依次為:long double、double、float、unsigned long long、long long、unsigned long、unsigned int、long、int

3. 在任何涉及兩種資料型別的操作中,它們之間等級較低的型別會被轉換成等級較高的型別。

C的「型別提公升」

寫在前面 本來因為乙個朋友問我為什麼可以給unsigned int 賦值負數,我打算寫一篇關於解釋 unsigned 多寶平台 char short int 或者int型 位段 無論signed 或unsigned 以及列舉型別 可以使用在需要 int或者 unsigned int 的表示式中。如果...

C語言裡的指標型別轉換

當我們初始化乙個指標或給乙個指標賦值時,賦值號的左邊是乙個指標,賦值號的右邊是乙個指標表示式。在我們前面所舉的例子中,絕大多數情況下,指標的型別和指標表示式的型別是一樣的,指標所指向的型別和指標表示式所指向的型別是一樣的。例 1 float f 12.3 2 float fptr f 3 int p...

c 語言裡 型別轉換那些事兒

一 起因 1 最近接觸了型別轉換,例如有符號數轉化為無符號數 int 轉化為 unsigned int型別,莫要想當然的轉化 2 彌補一下自己當初學習計算機組成原理時,原碼 反碼 補碼就比較容易混淆的概念 3 double型別的如何判斷某乙個引數是否為零,直接 0.0,貌似可以 確實有時可以,但是有...