用于表示 ANSI (C89/90) C 中的字节的类型?
是否有符合标准的方法来表示 ANSI (C89/90) C 中的字节? 我知道,大多数情况下,一个 char 恰好是一个字节,但我的理解是,不能保证情况一定如此。 还有,C99标准中有stdint.h,但是C99之前用的是什么?
我对 8 位和“字节”(sizeof(x) == 1)很好奇。
Is there a standards-complaint method to represent a byte in ANSI (C89/90) C? I know that, most often, a char happens to be a byte, but my understanding is that this is not guaranteed to be the case. Also, there is stdint.h in the C99 standard, but what was used before C99?
I'm curious about both 8 bits specifically, and a "byte" (sizeof(x) == 1).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
char
始终是一个 byte ,但它并不总是一个八位字节。 字节是内存的最小可寻址单元(在大多数定义中),八位字节是内存的 8 位单元。也就是说,对于所有实现,
sizeof(char)
始终为 1,但limits.h
中的CHAR_BIT
宏定义平台的一个字节的大小,并不总是 8 位。 有些平台有 16 位和 32 位字节,因此 char 会占用更多位,但它仍然是一个字节。 由于char
所需的范围至少为 -127 到 127(或 0 到 255),因此在所有平台上它至少为 8 位。强调我的。
char
is always a byte , but it's not always an octet. A byte is the smallest addressable unit of memory (in most definitions), an octet is 8-bit unit of memory.That is,
sizeof(char)
is always 1 for all implementations, butCHAR_BIT
macro inlimits.h
defines the size of a byte for a platform and it is not always 8 bit. There are platforms with 16-bit and 32-bit bytes, hencechar
will take up more bits, but it is still a byte. Since required range forchar
is at least -127 to 127 (or 0 to 255), it will be at least 8 bit on all platforms.Emphasis mine.
您始终可以用无符号字符表示一个字节(如果您指的是 8 位)。 它的大小始终至少为 8 位,所有位都构成该值,因此 8 位值始终适合它。
如果你想要正好 8 位,我也认为你必须使用依赖于平台的方式。 POSIX 系统似乎需要来支持 int8_t。 这意味着在 POSIX 系统上,char(以及字节)始终是 8 位。
You can always represent a byte (if you mean 8bits) in a unsigned char. It's always at least 8 bits in size, all bits making up the value, so a 8 bit value will always fit into it.
If you want exactly 8 bits, i also think you'll have to use platform dependent ways. POSIX systems seem to be required to support int8_t. That means that on POSIX systems, char (and thus a byte) is always 8 bits.
在 ANSI C89/ISO C90 中,sizeof(char) == 1。但是,1 个字节并不总是 8 位。 如果您希望计算 1 个字节中的位数(并且您无权访问 limit.h),我建议如下:
这里我们使用 Kernighan 的方法来计算 c。 为了更好地理解上面的代码(或查看其他类似的代码),我建议您参考“Bit Twiddling Hacks”。
In ANSI C89/ISO C90 sizeof(char) == 1. However, it is not always the case that 1 byte is 8 bits. If you wish to count the number of bits in 1 byte (and you don't have access to limits.h), I suggest the following:
Here we use Kernighan's method to count the number of bits set in c. To better understand the code above (or see others like it), I refer you to "Bit Twiddling Hacks".
C99之前? 与平台相关的代码。
但你为什么关心呢? 只需使用 stdint.h 即可。
在使用的每一个 CI 实现中(从旧的 UNIX 到硬件工程师编写的嵌入式编译器,再到大供应商编译器),
char
始终是 8 位。Before C99? Platform-dependent code.
But why do you care? Just use stdint.h.
In every implementation of C I have used (from old UNIX to embedded compilers written by hardware engineers to big-vendor compilers)
char
has always been 8-bit.你可以在 boost.h 中找到相当可靠的宏和 typedef。
You can find pretty reliable macros and typedefs in boost.
我注意到有些回答已经重新定义了“字节”一词,以表示除 8 位之外的其他内容。
一个字节是 8 位,但是在某些 C 实现中 char 是 16 位(2 个字节)或 8 位(1 个字节)。 那些将字节称为“最小可寻址内存单元”或类似垃圾的人已经失去了对字节(8 位)的含义的理解。
C 的某些实现具有 16 位字符(2 个字节),某些实现具有 8 位字符(1 个字节),并且没有称为“字节”的标准类型,这是由于懒惰。
所以,我们应该使用 int_8
I notice that some answered have re-defined the word byte to mean something other than 8 bits.
A byte is 8 bits, however in some c implementations char is 16 bits (2 bytes) or 8 bits (1 byte). The people that are calling a byte 'smallest addressable unit of memory' or some such garbage have lost grasp of the meaning of byte (8 bits).
The reason that some implementations of C have 16 bit chars (2 bytes) and some have 8 bit chars (1 byte), and there is no standard type called 'byte', is due to laziness.
So, we should use int_8