Loading

4-11.1 C++ 整型的隐式类型转换

  • 整型在运算时小类型会自动转换为大类型【保证精度】
  • 右值给左值赋值时,会被转换为左值类型
int i;
long long j;
int res = i+j;  //i转换为long long,i+j的类型是long long,给res初始化时又转为int
  • 有符号与无符号整型预算时,谁的类型大,容得下结果,就转换为谁,都能容下时以signed为主,保障精度
    • sizeof(unsigined type) >= sizeof(signed type): singned 转为 unsigned
    • sizeof(unsigned type) < sizeof(signed type):看结果是否能被signed容下,可以则unsigned转为signed,否则反之
      • long(32 bits) + unsigned int(32 bits):long ——> unsigned int
      • long(64 bits) + unsigned int(32 bits):unsigned int ——> long
posted @ 2022-01-18 11:49  咪啪魔女  阅读(45)  评论(0编辑  收藏  举报