C语言uint8_t和char的区别,有没有必要用uint8_t替换掉char呢,貌似很少有人这么

博客探讨了字节的定义在不同历史时期和标准下的复杂性,指出字节的位数不总是8,例如早期计算机可能使用6位字节。C语言标准定义了CHAR_BIT宏,但并未强制规定其值,而POSIX标准则要求CHAR_BIT为8,并暗示小型整型的表示必须是两补制。文章还提到了int8_t等固定宽度类型在不同规范中的应用。

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

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

Jens Gustedt's Blog How many bits has a byte? I recently stumbled about this seemingly silly question when trying to write a C macro that depends on the width of a type. So everybody knows the short answer, 8, as is also expressed in the commonly used French word for byte: `octet’. But surprisingly for me the long answer is more complicated than that: it depends on the historical context, the norms that you apply, and even then you have to dig a lot of text to come to it. C has a definition of the type char, and the language specification basically uses the terms char and byte interchangeably. Historically, in times there have been platforms with chars (and thus bytes) that had a width different from 8, in particular some early computers coded printable characters with only 6 bits and had a word size of 36. And later other constructors found it more convenient to have words of 16 bits to be the least addressable unit. C90 didn’t wanted to exclude such platforms and just stated The number of bits in a char is defined in the macro CHAR_BIT CHAR_BIT can be any value but must be at least 8 and even C99 still just states: A byte contains CHAR_BIT bits, and the values of type unsigned char range from 0 to (2^CHAR_BIT) – 1. But then, on the page for the include file stdint.h it states The typedef name int N _t designates a signed integer type with width N, no padding bits, and a two’s-complement representation. Thus, int8_t denotes a signed integer type with a width of exactly 8 bits. So far so good, if there is an int8_t we can deduce that sizeof(int8_t) must be 1 and CHAR_BIT must be 8. But then the POSIX standard says The following types are required: int8_t int16_t int32_t uint8_t uint16_t uint32_t Which forces CHAR_BIT to be 8, and basically also implies that at least for small width types the representation must be two’s-complement on any POSIX compatible platform.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值