unsigned char与char的区别

本文详细解析了C++中char类型的两种形式:signed char 和 unsigned char 的数值范围及使用场景,并提供了判断char类型是否为signed的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习一点轻松的内容。

有意思,今天发现我这里写的一篇文章居然没有了,或许是自己根本就没有发上来吧

就补充一下:

两者关系很简单,unsigned对应的数值是0~255,而signed是-128~127

查看msdn的话就可以看到了:

Character values of type unsigned char have a range from 0 to 0xFF hexadecimal. A signed char has range 0x80 to 0x7F. These ranges translate to 0 to 255 decimal, and –128 to +127 decimal, respectively. The /J compiler option changes the default from signed to unsigned.

char 是有或者为无符号的,是要通过设置的
unsigned char 是无符号的,里面全是正数

两者都作为字符用的话是没有区别的,因为现在的字符集有限,但当整数用时有区别,区别就是数值的对应

在c++里可以这么查看一个char定义是signed与否:

 

#include <limits>
#
include <iostream>
using namespace std;

int main(void) {
 cout 
<< "largest long is:" << numeric_limits<long>::max() 
  
<< "char is signed?" << numeric_limits<char>::is_signed 
  
<<endl;
 
return 1;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值