C 中的精确宽度整数类型 (stdint.h)
我希望 char、short 和 int 类型的宽度为 1、2 和 4 个字节。我已将 stdint.h 标头包含到我的源代码中。这是否保证 int8_t、int16_t 和 int32_t 整数类型具有指定的宽度?实现这一目标的最佳方法是什么?
I would like char, short and int types to be 1, 2 and 4 bytes width. I have included a stdint.h header into my sources. Does this guarantee that int8_t, int16_t and int32_t integer types will be of widths specified? What is the best way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这些类型存在,则它们具有正确的宽度并以二进制补码编码。
编辑:您应该能够检查类型是否存在,
但在所有最终用户架构(PC 等)上,这些类型通常确实存在。
If these types exist, they have the correct width and are encoded in two's complement.
Edit: you should be able to check if the types exist with something like
but on all end user architectures (PC etc) these types do usually exist.
stdint.h
中定义的类型的全部目的是具有指定的宽度。因此,int8_t
将为 8 位宽,int16_t
- 16 位宽,等等。The whole purpose of the types defined in
stdint.h
is of them to be of specified width. Soint8_t
will be 8 bits wide,int16_t
- 16 bits wide, etc.stdint.h
一些精确宽度的整数类型。这些是(来自 wikipedia):如果您想知道尺寸的用途,请使用它们定。 C99 要求这些类型具有指定的大小。所有其他类型均取决于平台。
其他类型保证具有特定的最小宽度,根据
limits.h
。但它们同样可以比这更大。这取决于实施。stdint.h
some exact-width integer types. These are (from wikipedia):Use them if you want to know what the size is for definite. C99 requires that these types have the sizes specified. All other types are platform dependent.
Other types are guaranteed to be a certain minimum width, as per
limits.h
. But they could equally be bigger than that. It is up to the implementation.C99 的 stdint.h 实际上并不保证这些类型存在。引用维基百科:
但是,如果它们确实存在,则它们将被正确的宽度。
C99's stdint.h does not actually guarantee that these types exist. To quote wikipedia:
However, if they do exist, they will be the correct widths.