类型转换
1. 显示转换
//char的范围是-128~127
char a = (char)200 //-56 //200-56 如果不加(char),在运行时会有警告
int b = (int)12.5
2. 隐式转换
char c = 100;
int d = c + 25 //125
隐式转换条件:
1.)默认由窄的数据类型转换为宽的数据类型 //判断宽窄的条件是字节数
//char字节数是1,int是4,所以char可以转int
2.)同级别的无符号比有符号宽 //eg:unsigned int 宽于 int
//所以只有 int 可以转换为 unsigned int
unsigned int a = 10;
int b = -2;
// b > a
//将int转换为unsigned int 2^32-1
3.)不同类型运算时,从 int 起
ps: 窄变宽,左边补符号位
无符号位(unsigned),左边补0
char a = -1;//-1
char b = 255;//-1 //255-256
unsigned char c = a;//255
unsigned char d = b;//255
int e = a;//-1
int f = b;//-1
unsigned int g = a;//2^32 -1
unsigned int h = b;//2^32-1