char 类型是有符号还是无符号?

char 类型是有符号还是无符号?

  对于这个问题,有些功底不深的同学可能会回答“char 类型是有符号类型”,理由是省略 signed 是可以的。

  而正确答案是:C99标准并未指定 char 类型是有符号还是无符号,这取决实现(编译器和硬件平台)。具体可以通过查看limits.h头文件中 CHAR_MIN 的值来确认,如果 CHAR_MIN 为 0 则说明 char 类型被当作无符号整型。

  关于这一点可以参考C99标准中的5.2.4.2.1小节,此处仅给出如下部分:
  If the value of an object of type char is treated as a signed integer when used in an expression, the value of CHAR_MIN shall be the same as that of SCHAR_MIN and the value of CHAR_MAX shall be the same as that of SCHAR_MAX. Otherwise, the value of CHAR_MIN shall be 0 and the value of CHAR_MAX shall be the same as that of UCHAR_MAX. The value UCHAR_MAX shall equal 2CHAR_BIT − 1.

  需要特别说明的一点是,无论 char 类型是被当作有符号还是无符号来处理,它与 signed char 和 unsigned char 都是不同的类型——即 char 、signed cahr 和 unsigned char 是三种不同的类型。可以参考C99标准 6.2.5 类型

  这里我以常用的51单片机和STM32单片机在 keil 下编译为例,在 keil C51下编译51单片机的代码时,char 类型是当作有符号处理的。

在这里插入图片描述

  而在 keil MDK 下编译stm32f407单片机的代码时,char 类型是当作无符号处理的。

在这里插入图片描述

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值