C语言 - 有符号数与无符号数

  1. 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	
  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 ;
  1. 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临时量的综合 报错,
				不允许对一个常量进行修改操作 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值