c语言的隐式转换

此次测试是在ubuntu下

对于C语言数据类型

sizeof

char 1个字节

short 2个字节

int 4个字节

long 4个字节

float 4个字节

double 8个字节

本人通过使用两个不同数据类型的值相加,然后使用sizeof观察其字节长度,判断转为什么类型了

short + char = 2byte= short

int + char 4byte = int

float + char = 4byte = float


short + int = 4byte int

long + int = 4byte=?

long long + int = 8byte long long


short + float = 4float = float

int + float 4byte = ?

long long + float = 4byte = float

double + float = 8byte + double


由此可以得出结论:

隐式转换的优先级有低到高为

字符型   char

   |

  \/

整型 short->int->long

  |

  \/

浮点型 float->double

由此看出

隐式转化的优点就是保护数据精确度,可能向精度更高的类型转换。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章