-
question1:char,int,short与unsigned int
进行比较或加减乘除的时候,数据类型是怎么提升的?
回答:
char short int long int + unsigend int =>unsigend int
unsigend char + int => int;
int char short int long + float double =>double
char a = -5;//有符号数
unsigned int x = 10; //无符号数
为什么 a > x呢?
因为x是无符号数 所以a>x的 结果也是无符号数
从无符号角度看-5=>10000101是一个非常大的数字:128+5
当输入:11111111时,
从无符号数角度结果是:unsigned char =>255
从有符号数角度结果是:char =>-1
- question2:无符号整型:u
sizeof(int); //sizeof算出来的是4字节的无符号整形:4u
int x =sizeof(int);//赋值过后会变成有符号数 x = 4;
若
for(int i = -4;i<4u;++i) //打印不出来 因为有符号i 与无符号4u 运算还是无符号
//所以i变成无符号数
// 有符号-4变成无符号之后 会变成一个非常非常大的数
char c = 128; // 10000000 结果是-128 警惕char 128!
unsigned char uc = 128; // 10000000
unsigned char short us = 0;
us = c + uc;
printf("%x\n",us);//=>0
us = (unsigned char)c+ uc;
printf("%x\n",us); //=>100
us = c + (char)uc;
printf("%x\n",us); //=>ff00
us = (unsigned short)c+ uc;
printf("%x\n",us);//0
return 0 ;
- question3:临时量具有常性
int a = 10;
char ch = 'a';//97
a = ch; //不允许对临时量进行写操作 可以读取--常性 --只读不写
int a =10,b=20;
int c ;
c = a+b; //将a赋给临时量,b也赋给临时量 临时量的总和赋给c
a+b = c; //a赋给临时量 b赋给临时量 c赋给a和b临时量的综合 报错,
不允许对一个常量进行修改操作