输入要使用重新present一个字节的ANSI(C89 / 90)C? [英] Type to use to represent a byte in ANSI (C89/90) C?
问题描述
是否有一个标准的投诉的方法来重新present一个字节的ANSI(C89 / 90)C?我知道,很多时候,一个char恰好是一个字节,但我的理解是,这是不能保证的情况下。此外,还有stdint.h在C99标准,但C99前使用的是什么?
我很好奇这两个专8位,和一个字节(的sizeof(X)== 1)。
字符
总是字节,但它并不总是一个<强>字节的。字节是内存的最小可寻址单元(在大多数的定义),一个八位字节的内存8位单位。
这就是的sizeof(char)的
总是 1 所有的实现,但 CHAR_BIT
在 limits.h中
定义了一个平台一个字节的大小,它并不总是8位。有16位和32位字节的平台,因此,字符
将占用更多的位,但它仍然是一个字节。由于对字符
要求的范围至少-127到127(或0到255),这将是的至少的8位在所有平台上。
ISO / IEC 9899:TC3
6.5.3.4 sizeof操作符
- ...
- sizeof操作符产生的操作数(以字节为单位的 )的大小,这可能是一个前pression或类型的括号名称。 [...]
- 当应用到具有类型的操作数
字符
,unsigned char型
或签署焦
,(或合格网络版版本体),结果为 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).
解决方案
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.ISO/IEC 9899:TC3
6.5.3.4 The sizeof operator
- ...
- The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. [...]
- When applied to an operand that has type
char
,unsigned char
, orsigned char
, (or a qualified version thereof) the result is 1. [...]
Emphasis mine.
这篇关于输入要使用重新present一个字节的ANSI(C89 / 90)C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!